Understanding Variables and Data Types in JavaScript
Master the different ways to declare variables and understand JavaScript data types.
Variable Declaration
JavaScript provides three ways to declare variables:
let: Block-scoped, can be reassigned
let age = 25;
const: Block-scoped, cannot be reassigned
const name = "John";
var: Function-scoped (older way, avoid using)
var count = 0;
Primitive Data Types
JavaScript has 7 primitive data types:
- String: Text data ("Hello")
- Number: Numeric values (42, 3.14)
- Boolean: true or false
- undefined: Uninitialized variables
- null: Intentional absence of value
- Symbol: Unique identifiers
- BigInt: Large integers
Type Checking
Use typeof to check variable types:
typeof "hello" // "string"
typeof 42 // "number"
typeof true // "boolean"
Best Practices
- Use const by default
- Use let only when you need to reassign
- Avoid var in modern JavaScript
- Use meaningful variable names