JavaScript Data Types
In JavaScript, data types define the type of value a variable can hold. JavaScript is dynamically typed, meaning a variable can hold different types of values at different times.
1️⃣ Primitive Data Types (Immutable & Stored by Value)
JavaScript has 7 primitive data types:
Data Type | Example | Description |
---|---|---|
Number | let x = 10; | Stores integers & floating-point numbers. |
String | let name = "Alice"; | Text enclosed in quotes (" " or ' ' or ```````). |
Boolean | let isReady = true; | true or false values. |
Undefined | let y; | A variable declared but not assigned a value. |
Null | let z = null; | Represents "nothing" or an empty value. |
BigInt | let bigNum = 12345678901234567890n; | Used for very large numbers. |
Symbol | let sym = Symbol("id"); | Unique identifiers (mostly for objects). |
🔹 Example
2️⃣ Non-Primitive (Reference) Data Types
These types store references to objects in memory.
Data Type | Example | Description |
---|---|---|
Object | let person = {name: "John", age: 30}; | Key-value pairs. |
Array | let fruits = ["apple", "banana", "cherry"]; | Ordered list of values. |
Function | function greet() { return "Hello"; } | A block of code that can be executed later. |
🔹 Example
3️⃣ typeof Operator (Checking Data Types)
You can check a variable's data type using typeof
.
4️⃣ Special Cases
❗ null
is an object (typeof null
returns "object"
). This is a long-standing JavaScript bug.
❗ Arrays are also objects, but they have additional properties.
❗ Functions are objects with executable code.
🎯 Summary
✅ Primitive types: Number
, String
, Boolean
, Undefined
, Null
, BigInt
, Symbol
✅ Non-primitive types: Object
, Array
, Function
✅ Use typeof
to check a variable's type
✅ Objects and arrays are stored as references in memory
🚀 Now you're ready to handle data types like a pro! Let me know if you need more details. 😊