JS Data Types
In this page:
String, Number, and Boolean
string represents text wrapped in quotes, number represents both whole and decimal numbers using a single type, and boolean represents only true or false, the three most commonly used data types in everyday JavaScript.
Note: Unlike many languages, JavaScript uses one number type for both integers and decimals, there's no separate int vs float.
Warning: Numbers written without quotes and text written with quotes look similar but behave very differently in arithmetic and comparisons.
Example: String, Number, and Boolean
let name = "Alice";
let age = 30;
let isActive = true;
console.log(typeof name, typeof age, typeof isActive);
undefined and null
undefined means a variable has been declared but never given a value, while null is an intentional value developers assign to represent nothing or 'empty on purpose', they represent different concepts despite both meaning an absence of a real value.
Note: A helpful mental model, undefined usually means the language did this automatically, null usually means a developer set this on purpose.
Warning: typeof null returns object, a famous historical bug in JavaScript that's been kept for compatibility rather than fixed.
Example: undefined and null
let notSet;
let empty = null;
console.log(notSet); // undefined
console.log(empty); // null
bigint and symbol
bigint represents whole numbers larger than the regular number type can safely handle, written with an n suffix, while symbol creates a guaranteed unique value, often used as a special, collision-free property key.
Note: bigint is only needed for extremely large integers, calculations involving very large IDs or precise large-number math are common use cases.
Warning: You cannot mix a regular number and a bigint directly with arithmetic operators, doing so throws a TypeError.
Example: bigint and symbol
let big = 12345678901234567890n;
let id = Symbol("id");
console.log(typeof big, typeof id);
The object Type
object is the only non-primitive type, used to group related data and functionality together, and it covers not just plain objects but also arrays, functions, and dates, all of which report as object or function with typeof.
Note: When you need to store multiple related pieces of information together, like a user's name and email, an object is usually the right type.
Warning: Arrays report typeof as object, not array, use Array.isArray() specifically when you need to confirm something is an array.
Example: The object Type
let user = { name: "Sam" };
let list = [1, 2, 3];
console.log(typeof user, typeof list);
Dynamic Typing
JavaScript is dynamically typed, meaning a variable's type is not fixed when declared, it's determined by whatever value currently sits inside it, and that value, along with its type, can change if the variable is reassigned.
Note: Dynamic typing offers flexibility, but writing consistent code, keeping a variable's intended type stable over its lifetime, reduces confusing bugs.
Warning: Accidentally reassigning a variable to an unexpected type, like changing a number to a string mid-script, can cause subtle downstream errors.
Example: Dynamic Typing
let value = 5;
console.log(typeof value); // "number"
value = "now a string";
console.log(typeof value); // "string"
- Assuming null and undefined are the same thing, they are related but distinct values with different intended meanings.
- Forgetting that JavaScript is dynamically typed, meaning a variable's type can change if you reassign it to a different kind of value.
- Using typeof to check for null, since typeof null incorrectly returns object due to a long-standing language quirk.
- JavaScript's primitive types are string, number, bigint, boolean, undefined, null, and symbol, plus the non-primitive object type.
- typeof is the standard way to check a value's type at runtime.
- JavaScript is dynamically typed, a variable's type is determined by its current value and can change over time.
All core JavaScript data types are part of the language standard and behave identically across every modern browser; BigInt is supported in all current browser versions.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: