JavaScript Data Types
Every value in JavaScript has a type. There are eight of them: seven primitive types for simple values, and object for everything structured — including arrays, functions and dates.
| Type | Example values | typeof result |
|---|---|---|
| String | "hello", 'a', `hi ${name}` |
"string" |
| Number | 42, 3.14, -7, NaN, Infinity |
"number" |
| BigInt | 9007199254740993n |
"bigint" |
| Boolean | true, false |
"boolean" |
| Undefined | undefined |
"undefined" |
| Null | null |
"object" (a historic bug) |
| Symbol | Symbol("id") |
"symbol" |
| Object | { a: 1 }, [1, 2], new Date(0) |
"object" (functions: "function") |
console.log(typeof "text");
console.log(typeof 3.5);
console.log(typeof 10n);
console.log(typeof false);
console.log(typeof undefined);
console.log(typeof null);
console.log(typeof Symbol("id"));
console.log(typeof { a: 1 }, typeof [1, 2], typeof function () {});string
number
bigint
boolean
undefined
object
symbol
object object function
Dynamic typing
Variables don't have types — values do. The same variable can hold different types over time, and JavaScript converts between types automatically in many operations:
let data = 5;
console.log(data + 5); // number + number
data = "5";
console.log(data + 5); // string + number -> string
console.log(data * 2); // * converts the string to a number10
55
10
This flexibility is convenient but can surprise you; the Type Conversion chapter explains the rules.
Strings, numbers and booleans
- Strings hold text. Use single quotes, double quotes, or backticks; backticks allow
${…}placeholders. - Numbers cover integers and decimals in one type (64-bit floating point). Very large integers need BigInt.
- Booleans are
trueorfalse— the result of comparisons.
const product = "Desk lamp";
const price = 29.99;
const inStock = price < 50;
console.log(`${product}: ${price} (${inStock ? "affordable" : "pricey"})`);
console.log(2 ** 53 + 1); // beyond safe integer range — wrong!
console.log(2n ** 53n + 1n); // BigInt is exactDesk lamp: 29.99 (affordable)
9007199254740992
9007199254740993n
undefined vs null
Both mean "no value", but they're used differently:
undefined— the value hasn't been set: an unassigned variable, a missing property, a function with noreturn.null— the value has been intentionally set to nothing by the programmer.
let middleName;
const user = { name: "Kim", manager: null };
console.log(middleName);
console.log(user.manager);
console.log(user.phone); // property doesn't exist
console.log(null == undefined, null === undefined);undefined
null
undefined
true false
Primitives vs objects
Primitive values are immutable and are copied by value. Objects are copied by reference — two variables can point to the same object:
let a = "hi";
let b = a;
b = b + "!";
console.log(a, b); // a is unaffected
const list1 = [1, 2];
const list2 = list1; // same array, two names
list2.push(3);
console.log(list1, list2);hi hi!
[ 1, 2, 3 ] [ 1, 2, 3 ]
Tip:
typeofcan't tell arrays from plain objects. UseArray.isArray(value)to check for arrays, andvalue === nullto check for null.
Test yourself
What does typeof null return?
- "null"
- "undefined"
- "object"
- "none"
How many primitive types does JavaScript have?
- 5
- 6
- 7
- 8
What does typeof [1, 2, 3] return?
- "array"
- "object"
- "list"
- "number"