0.2 Primitive & Complex Data Types
Every piece of information in JavaScript has a Data Type. Understanding types and how JavaScript automatically converts them is essential to writing bug-free code.
1. The 7 Primitive Data Types
Primitives are basic, single values stored directly in memory:
Number: Integers and decimals (e.g.42,3.14,-10)String: Text enclosed in quotes (e.g."Hello",'JS',`Template`)Boolean: Logical truth values (trueorfalse)Undefined: A variable declared but assigned no value yetNull: Intentional empty value (explicitly empty)BigInt: Super large integers beyond standard Number rangeSymbol: Unique, immutable identifier
2. Complex Types: Objects & Arrays
Unlike primitives, complex data structures can store multiple values grouped together:
// Object: Key-Value pairs
let person = { name: "Alex", age: 25 };
// Array: Ordered list of values
let colors = ["Red", "Green", "Blue"];
let person = { name: "Alex", age: 25 };
// Array: Ordered list of values
let colors = ["Red", "Green", "Blue"];
3. Type Coercion (Implicit vs Explicit)
JavaScript is dynamically typed. Variables can hold any type, and JS often auto-converts types (Implicit Coercion):
"10" + 5→"105"(Plus operator concatenates strings!)"10" - 5→5(Minus operator forces numeric conversion!)- Explicit Conversion:
Number("10"),String(42),Boolean(1)
📌 KEY POINTS TO REMEMBER
- `null` vs `undefined`:
undefinedmeans "uninitialized by system", whereasnullmeans "deliberately cleared by programmer". - Historical JS Bug:
typeof nullreturns"object"due to an early 1995 implementation quirk in JavaScript. - `typeof` operator: Use
typeof myVarto check any variable's data type at runtime.
⚡ Interactive Visual: Coercion & Type Explorer
Test how JS evaluates implicit coercion and check `typeof` outputs:
Click any test button above.