Modern Variables in JavaScript
Since ES6 (2015), we have three ways to declare a variable. Each handles scope differently.
javascript
Comparisons: let vs const vs var
| Feature | let | const | var |
|---|---|---|---|
| Scope | Block Scope {} | Block Scope {} | Function Scope function() |
| Reassignable | Yes | No | Yes |
| Redeclarable | No | No | Yes |
| Hoisting | No | No | Yes (moved to top of code!) |
Best Practice: Always useconstby default. Only useletwhen you're 100% sure the variable's value needs to change later in your code. Never usevar!
Primitive Data Types
Primitive types are basic building blocks that represent a single value.
- Number: Handles both integers and decimals.
- String: Text content wrapped in
',", or`(backticks). - Boolean:
trueorfalse. - Null: A deliberate assignment to "nothingness."
- Undefined: The value automatically assigned to an uninitialized variable.
- Symbol: Unique identifier (mostly for advanced internal usage).
- BigInt: For numbers larger than the standard MAX_SAFE_INTEGER.
javascript
Object Data Types
Objects are used to store collections of data.
javascript
Wait! null is an Object?
This is a famous bug in JavaScript's history.
typeof null returns "object", but it's actually a primitive.javascript
Variable Naming (Case Sensitivity)
JavaScript variables are case-sensitive. Use camelCase for variable and function names.
javascript
Summary
| Type | Examples | Description |
|---|---|---|
| let | let x = 1 | Reassignable, block-scoped |
| const | const x = 1 | Immutable reference, block-scoped |
| Number | 10, 3.14 | Both integers and floats |
| String | "Hi", 'Hi' | Text |
| Boolean | true, false | Logic |
| Null/Undefined | — | Nothingness |
| Object | {a: 1} | Structured data |
| Array | [1, 2] | Lists of data |
| Best For | Data storage, variables, and constants | |
| Primitive | Fixed-size values | |
| Reference | Objects, Arrays, Functions (stored in memory heap) | |
| Template Literal | `Value: ${val}` | Clean string merging |
| Variable Mutation | const obj = {}; obj.a = 1; (Allowed — reference is unchanged!) | |
| Dynamic Type | let x = 1; x = "hi"; (Allowed — flexibility!) | |
| Symbol() | Uniqueness | |
| BigInt() | 100n | Large-scale integers |
| typeof | Identifier | |
| instanceof | Context |