Data Types¶
JavaScript has 8 basic data types, divided into primitive and reference (non-primitive) types.
1. Primitive Types (Immutable)¶
Stored directly in the variable.
a) String
¶
Text data.
let name = "Henry";
b) Number
¶
Both integers and floats.
let age = 15;
let pi = 3.14;
c) Boolean
¶
Logical values.
let isOnline = true;
d) undefined
¶
A variable declared but not assigned.
let x;
console.log(x); // undefined
e) null
¶
Intentional absence of value.
let data = null;
f) Symbol
(ES6)¶
Unique and immutable identifier.
let sym = Symbol("id");
g) BigInt
(ES2020)¶
Handles large integers beyond Number.MAX_SAFE_INTEGER
.
let big = 1234567890123456789012345678901234567890n;
2. Reference Types (Objects)¶
Stored as references in memory.
a) Object
¶
Collection of key-value pairs.
let user = {
name: "Aman",
age: 15
};
b) Array
¶
Indexed list of values.
let scores = [90, 85, 100];
c) Function
¶
Functions are objects with callable behavior.
function greet() {
console.log("Hi");
}
3. Type Checking¶
Use typeof
to check types:
typeof "hello"; // "string"
typeof 42; // "number"
typeof null; // "object" (bug in JS)
typeof []; // "object"
typeof undefined; // "undefined"
To check arrays:
Array.isArray([1, 2, 3]); // true
4. Dynamic Typing¶
JavaScript is dynamically typed, so variables can change type at runtime.
let x = "hello";
x = 123; // valid