Overview of Data types
A data type describes what kind of value something is: a number, a piece of text, true or false, an object and so on. JavaScript has eight data types. Seven are primitive, meaning simple values that cannot be changed, and one is the object type, which covers arrays, functions, dates and every other structured value. Variables in JavaScript do not have fixed types; the value inside them does.
The eight types at a glance
| Type | Example values | Used for |
|---|---|---|
| Number | 42, 3.14, -7, NaN, Infinity | Integers and decimals alike. |
| BigInt | 12345678901234567890n | Whole numbers too large for Number. |
| String | 'hello', "Asha", `Hi ${name}` | Text. |
| Boolean | true, false | Yes or no decisions. |
| Undefined | undefined | A variable declared but not given a value. |
| Null | null | An intentional "no value". |
| Symbol | Symbol('id') | Unique keys for object properties. |
| Object | { id: 1 }, [1, 2], new Date() | Collections and structured data. |
Checking types with typeof
console.log(typeof 42); // "number"
console.log(typeof 'hi'); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof 10n); // "bigint"
console.log(typeof Symbol()); // "symbol"
console.log(typeof { a: 1 }); // "object"
console.log(typeof [1, 2]); // "object" (arrays are objects)
console.log(typeof function () {});// "function" (a callable object)
console.log(typeof null); // "object" (a historic quirk)
console.log(typeof NaN); // "number"
let value = 5; // a number now
value = 'five'; // a string now: allowed, types belong to values
console.log(Array.isArray([1, 2])); // true
console.log(value === null); // the reliable null check
Primitives versus objects
The split between primitives and objects affects how values behave when you copy or compare them.
- Primitives are copied by value.
let a = 5; let b = a; b = 9;leavesaas5. Primitives are also immutable: string methods liketoUpperCase()return a new string instead of changing the old one. - Objects are shared by reference.
const x = { n: 1 }; const y = x; y.n = 2;changesx.ntoo, because both variables point to one object. - Comparison follows the same rule. Two strings with the same text are equal. Two separate objects with identical contents are not:
{} === {}isfalse.
Quirks worth memorising
typeof null returns "object". It is a long-standing mistake in the language that cannot be fixed without breaking old websites. To test for null, compare directly with value === null. To test for an array, use Array.isArray(), since typeof says "object".
NaN, meaning "not a number", is itself of type number. It appears after invalid maths like Number('abc'), and it is not equal to anything, even itself. Check it with Number.isNaN(value).
Finally, there is no separate integer type. 5 and 5.0 are the same Number. That is convenient, but it means very large integers lose precision, which is why BigInt exists.
undefined and null are not the same
JavaScript gives you undefined automatically: an unassigned variable, a missing object property or a function with no return. null is something you set on purpose to say "empty for now", for example clearing a selected item. Many teams use null for deliberate blanks and let undefined mean "never set".
Summary
JavaScript has seven primitive types and one object type. Primitives are copied and compared by value; objects by reference. Use typeof for most checks, but test null with === and arrays with Array.isArray. The next lesson goes through each primitive type in more detail.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.