JavaScript Data Types
JavaScript has 8 data types: 7 primitive types and 1 object type. The typeof operator returns the type of a value.
| Type | Example Value | typeof Result |
|---|---|---|
| Number | 42, 3.14 | "number" |
| BigInt | 12345678901234567890n | "bigint" |
| String | "Hello", 'World' | "string" |
| Boolean | true, false | "boolean" |
| Undefined | let x; | "undefined" |
| Null | null | "object" (historical bug) |
| Symbol | Symbol("id") | "symbol" |
| Object | {}, [], new Date() | "object" |
Numbers
Number Type
let integer = 42;
let decimal = 3.14;
let negative = -100;
console.log(typeof integer); // "number"
console.log(typeof decimal); // "number"
console.log(integer + decimal);Strings
String Type
let single = 'Hello';
let double = "World";
let template = `${single} ${double}!`; // template literal
console.log(single);
console.log(double);
console.log(template);Booleans
Boolean Type
let isOpen = true;
let isClosed = false;
console.log(typeof isOpen); // "boolean"
console.log(10 > 5); // true
console.log(10 < 5); // falseUndefined and Null
Undefined vs Null
let a; // declared but not assigned
let b = null; // intentionally empty
console.log(a); // undefined
console.log(typeof a); // "undefined"
console.log(b); // null
console.log(typeof b); // "object" (historical JS bug)
console.log(a === b); // falseObjects and Arrays
Object and Array Types
const person = { name: "Alice", age: 30 };
const colors = ["red", "green", "blue"];
console.log(typeof person); // "object"
console.log(typeof colors); // "object"
console.log(Array.isArray(colors)); // trueDynamic Typing
JavaScript is dynamically typed. The same variable can hold values of different types.
Dynamic Typing
let value = 42;
console.log(typeof value); // "number"
value = "now a string";
console.log(typeof value); // "string"
value = true;
console.log(typeof value); // "boolean"📝 Note: When you add a Number and a String, JavaScript treats the number as a string: 5 + "5" = "55".
Exercise:
Which of these is NOT a JavaScript primitive data type?