JavaScript Tutorial · JavaScript Basics · Lesson 6 of 44
JavaScript Data Types
Strings, numbers, booleans, null, undefined, objects — and typeof.
Primitive types
JavaScript has seven primitive types:
| Type | Example |
|---|---|
string |
"hello", 'hi', `template` |
number |
42, 3.14, NaN, Infinity |
bigint |
9007199254740993n |
boolean |
true, false |
undefined |
a variable with no value yet |
null |
"intentionally empty" |
symbol |
Symbol("id") — unique keys |
Everything else — arrays, functions, dates, plain {} objects — is an object.
typeof
The typeof operator tells you the type of a value:
console.log(typeof "Ada");
console.log(typeof 42);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof null);
console.log(typeof [1, 2]);
console.log(typeof { a: 1 });
console.log(typeof function () {});
console.log(Array.isArray([1, 2]));
Output
string number boolean undefined object object object function true
Note the famous quirk: typeof null is "object" — a bug from 1995 that can never be fixed without breaking the web. Use value === null to test for null, and Array.isArray(value) to test for arrays.
Dynamic typing
A variable can hold any type, and can change type:
let value = 5;
console.log(value, typeof value);
value = "five";
console.log(value, typeof value);
Output
5 number five string
Test yourself
What does typeof null return?
"null""undefined""object""boolean"
Show answer
Answer: "object"
A historical bug: typeof null is "object". Compare with === null instead.