Every value in JavaScript has a type. The type tells JavaScript what the value is and what you can do with it. You can multiply numbers, turn strings into capital letters and check whether a boolean is true, but you can't sensibly multiply two pieces of text.
Understanding types helps you predict what your code will do. Many beginner bugs, such as a total of "510" instead of 15, happen because a value has a different type from the one you expected.
The primitive data types
| Type | Examples | Used for |
|---|---|---|
| string | "hello", 'cat', `hi` | Text |
| number | 42, 3.14, -7 | Any number, whole or decimal |
| boolean | true, false | Yes or no answers |
| undefined | undefined | "Nothing has been assigned yet" |
| null | null | "Deliberately empty" |
| bigint | 9007199254740993n | Very large whole numbers |
| symbol | Symbol("id") | Unique identifiers (advanced) |
Primitives are simple, single values. You'll use strings, numbers and booleans constantly; bigint and symbol are rare in everyday web code.
Objects and arrays
Everything that isn't a primitive is an object, which is a collection of values:
const pet = { name: "Pixel", age: 2 }; // object
const scores = [10, 25, 8]; // array (a special kind of object)Objects group related information under named keys, and arrays hold ordered lists. You'll learn both in detail in Objects and Arrays.
Checking a type with typeof
typeof tells you a value's type as a string:
typeof "hi" // "string"
typeof 42 // "number"
typeof true // "boolean"
typeof undefined // "undefined"
typeof {} // "object"
typeof [] // "object" (use Array.isArray to check for arrays)
typeof null // "object" (a famous old bug)Dynamic typing and type conversion
A JavaScript variable can hold any type of value and can switch to a different type later. That's flexible, but it's also a common source of bugs, such as "5" + 1 giving "51". Convert values explicitly when you need a particular type: Number("5"), String(5) or Boolean(1).
Values typed into forms always arrive as strings, even when the visitor types a number. Converting them with Number() before doing maths avoids a lot of confusing results.
JavaScript Data Types Syntax
Types at a glance
const text = "Hi"; // string
const count = 3; // number
const isOpen = false; // boolean
let nothing; // undefined
const empty = null; // null
const user = { id: 1 }; // object
const list = [1, 2, 3]; // array (object)JavaScript Data Types Summary
- Every value has a type that decides what you can do with it.
- The main primitive types are string, number, boolean, undefined and null.
- Objects and arrays hold collections of values.
typeofreports a value's type; useArray.isArray()for arrays.- Variables can change type, so convert values with
Number()orString()when needed.
JavaScript Data Types Examples
Each example runs in the page. Edit the code and press Run to see what changes.
Checking types with typeof
console.log(typeof "Pixel");
console.log(typeof 3.14);
console.log(typeof false);
console.log(typeof undefined);
console.log(typeof { a: 1 });
console.log(typeof [1, 2]);
console.log(Array.isArray([1, 2]));
console.log(typeof null);Line-by-line explanation
- Lines 1 to 4The four everyday primitives.
- Lines 5 to 6Objects and arrays both say "object".
- Line 7
Array.isArrayis the reliable way to detect arrays. - Line 8
typeof nullsays "object". It's a bug from 1995 that can't be fixed without breaking old websites.
Types change how operators behave
console.log(5 + 5);
console.log("5" + 5);
console.log("5" * 2);
console.log(Number("5") + 5);
console.log(String(10) + "px");
console.log(Boolean(""), Boolean("hi"), Boolean(0), Boolean(42));Line-by-line explanation
- Line 1Number + number = addition.
- Line 2If either side is a string,
+joins them: "55". - Line 3
*only works on numbers, so JavaScript converts "5" to 5. - Line 4Convert explicitly to avoid surprises.
- Line 5Number to string.
- Line 6Empty strings and 0 are "falsy"; most other values are "truthy". More in Comparisons.
Common JavaScript Data Types Mistakes
Adding numbers from inputs
const a = "10"; // values from inputs are strings!
const b = "5";
console.log(a + b); // "105"const a = Number("10");
const b = Number("5");
console.log(a + b); // 15Everything typed into a form field arrives as a string. Convert it before doing maths.
Checking for arrays with typeof
if (typeof items === "array") { … } // never trueif (Array.isArray(items)) { … }JavaScript Data Types in Real Projects
Types in real apps
Form values arrive as strings, API data arrives as JSON with numbers, booleans and nulls, and checkboxes give booleans. Many teams use TypeScript, which is JavaScript with type checking, to catch type mistakes before the code even runs.
JavaScript Data Types Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Complete the comments with each value's typeof result.
typeof "42" // ""
typeof 42 // ""
typeof true // ""
What does this print?
console.log("3" + 4 + 5);
console.log(3 + 4 + "5");What does typeof null return?
The total should be 25, but it prints 1510. Fix the code so the strings are converted to numbers.
const apples = "15";
const pears = "10";
const total = apples + pears;
console.log("Total fruit:", total);- Logs "Total fruit: 25"
- total is a number
JavaScript Data Types Quiz
3 questions to check what you learned.
Which of these is a boolean?
- "true"
- 0
- "no"
- false
What's the difference between
undefinedandnull?undefinedmeans no value was assigned;nullis deliberately empty- They're identical
nullis a numberundefinedis a string
How do you reliably check if a value is an array?
typeof x === "array"Array.isArray(x)x.type === "array"x instanceof List
JavaScript Data Types Challenge
Type detective
Log the type of each value using typeof, in this order, so the console shows:
string, number, boolean, undefined, object
Then log true using Array.isArray(colors).
const robotName = "Robo";
const battery = 87;
const isCharging = false;
let nickname;
const colors = ["silver", "blue"];
- Logs the five types in order
- Logs true for Array.isArray