🟢 JavaScript Fundamentals — Variables, Data Types & Type Conversion
Now that you understand the basic idea of programming and JavaScript, let's learn how JavaScript stores and works with different kinds of data.
1️⃣ Variables in JavaScript
A variable is a named container used to store a value.
let name = "Rahul";
let age = 25;
Here:
• "name" stores "Rahul"
• "age" stores "25"
You can use the variable later:
console.log(name);
console.log(age);
Output:
Rahul
25
2️⃣ "let", "const" and "var"
JavaScript provides three keywords for declaring variables.
"let"
Use "let" when the value may change.
let score = 50;
score = 80;
console.log(score);
Output:
80
"const"
Use "const" when you don't intend to reassign the variable.
const country = "India";
console.log(country);
This will cause an error:
country = "Japan"; // Error: Assignment to constant variable.
because a "const" variable cannot be reassigned.
"var"
"var" is the older way of declaring variables.
var city = "Pune";
Modern JavaScript generally prefers:
•
let
•
const
over "var".
3️⃣ Variable Naming Rules
Valid:
let firstName = "Rahul";
let age2 = 25;
let totalAmount = 5000;
Invalid:
let 2age = 25; // Error: Unexpected number
Variable names cannot start with a number.
Also avoid names like:
let let = 10; // Error: Unexpected token
because "let" is a reserved JavaScript keyword.
A common naming style is camelCase:
let firstName = "Rahul";
let totalMarks = 450;
let accountBalance = 50000;
4️⃣ JavaScript Data Types
A data type tells JavaScript what kind of value it is dealing with.
JavaScript has several built-in data types.
The most important ones initially are:
🔹 String
Used for text.
let name = "Rahul";
let message = 'Hello';
Strings can also use backticks:
let message = `Hello Rahul`;
🔹 Number
Used for numbers.
let age = 25;
let price = 999.50;
JavaScript uses the "number" type for both integers and decimal numbers.
🔹 Boolean
A Boolean has only two values:
• true
• false
Example:
let isLoggedIn = true;
let isAdmin = false;
Booleans are extremely important when working with conditions.
🔹 Undefined
A variable that has been declared but hasn't been given a value contains "undefined".
let result;
console.log(result);
Output:
undefined
🔹 Null
"null" represents an intentional absence of a value.
let selectedUser = null;
This means you are deliberately saying:
"There is currently no selected user."
5️⃣ Checking Data Types
JavaScript provides "typeof".
let name = "Rahul";
console.log(typeof name);
Output:
string
More examples:
console.log(typeof 25); // Output: number
console.log(typeof true); // Output: boolean
console.log(typeof undefined); // Output: undefined
One famous JavaScript quirk is:
typeof null; // returns: object
This is a historical behavior in JavaScript.
6️⃣ Primitive vs Reference Values
This distinction becomes very important later.
Primitive values include:
• 🔹 String
• 🔹 Number
• 🔹 Boolean
• 🔹 Undefined
• 🔹 Null
• 🔹 BigInt
• 🔹 Symbol
Objects, arrays, and functions are non-primitive/reference values.
For example:
let name = "Rahul";
let person = {
name: "Rahul",
age: 25
};
"name" contains a primitive value.
"person" is an object.
You'll understand the difference much more deeply when we cover objects, memory, and references.
7️⃣ Type Conversion
Sometimes you need to convert one data type into another.
For example:
let age = "25"; // Here "age" is a string, not a number.