Curriculum Series

JavaScript data types

JavaScript data types

JavaScript data types

JavaScript

The short answer

JavaScript has eight data types. Seven of them are primitives: string, number, bigint, boolean, undefined, null, and symbol. The eighth is object, which includes arrays, functions, dates, and regular objects.

Primitive types

Primitives are the simplest values. They are immutable — you cannot change them. When you assign a primitive to a new variable, it copies the value.

String — text data, wrapped in quotes:

JAVASCRIPT
1const name = 'John';
2const greeting = 'Hello';
3const template = `Hi ${name}`;

Number — integers and decimals, including special values:

JAVASCRIPT
1const age = 30;
2const price = 9.99;
3const result = Infinity;
4const invalid = NaN; // "Not a Number"

JavaScript uses a single number type for both integers and floating-point numbers. There is no separate int or float type.

BigInt — for numbers larger than what number can handle:

JAVASCRIPT
1const huge = 9007199254740991n;
2const alsoHuge = BigInt('9007199254740991');

You add n at the end to make it a BigInt. Regular numbers lose precision above 2^53 - 1, but BigInt can handle any size.

Booleantrue or false:

JAVASCRIPT
1const isActive = true;
2const isDeleted = false;

Undefined — a variable that has been declared but not assigned a value:

JAVASCRIPT
1let x;
2console.log(x); // undefined

Null — an intentional "no value." You use it to explicitly say "this has nothing":

JAVASCRIPT
1let user = null; // we know there is no user

Symbol — a unique identifier, mostly used for object property keys:

JAVASCRIPT
1const id = Symbol('id');
2const anotherId = Symbol('id');
3console.log(id === anotherId); // false — every symbol is unique

Object type

Everything that is not a primitive is an object. Objects are collections of key-value pairs, and they are stored by reference.

JAVASCRIPT
1// Regular object
2const user = { name: 'John', age: 30 };
3
4// Array (a special kind of object)
5const numbers = [1, 2, 3];
6
7// Function (also an object)
8function greet() {}
9
10// Date
11const now = new Date();

The key difference between primitives and objects is how they are stored. Primitives are stored by value — copying creates an independent copy. Objects are stored by reference — copying only copies the pointer.

JAVASCRIPT
1// Primitives — independent copies
2let a = 10;
3let b = a;
4b = 20;
5console.log(a); // 10 — not affected
6
7// Objects — shared reference
8let obj1 = { name: 'John' };
9let obj2 = obj1;
10obj2.name = 'Jane';
11console.log(obj1.name); // "Jane" — both point to the same object

typeof operator

You can check a value's type using typeof:

JAVASCRIPT
1typeof 'hello'; // "string"
2typeof 42; // "number"
3typeof true; // "boolean"
4typeof undefined; // "undefined"
5typeof Symbol(); // "symbol"
6typeof 42n; // "bigint"
7typeof {}; // "object"
8typeof []; // "object" — arrays are objects
9typeof null; // "object" — this is a known bug in JavaScript
10typeof function () {}; // "function"

Common Pitfalls

There are two well-known quirks with typeof. First, typeof null returns "object" instead of "null". This is a bug from the very first version of JavaScript that was never fixed. Second, typeof [] returns "object" because arrays are technically objects. To check for an array, use Array.isArray([]) instead.

Type coercion

JavaScript automatically converts types in certain situations, which can lead to surprising results:

JAVASCRIPT
1'5' + 3; // "53" — number becomes string
2'5' - 3; // 2 — string becomes number
3true + 1; // 2 — true becomes 1
4false + 1; // 1 — false becomes 0
5null + 1; // 1 — null becomes 0
6undefined + 1; // NaN

The + operator prefers strings (it concatenates), while -, *, / prefer numbers (they convert to numbers). This is why === (strict equality) is preferred over == (loose equality) — strict equality does not do type coercion.

Interview Tip

When answering this question, list all eight types and briefly explain each. Make sure to mention the typeof null bug and the difference between primitives (by value) and objects (by reference). If the interviewer asks follow-up questions, knowing about type coercion and how === vs == work will impress them.

Why interviewers ask this

This is a fundamental JavaScript question. Interviewers use it to check your baseline knowledge. They want to see if you know all eight types, if you understand the difference between primitives and objects, and if you are aware of the quirks like typeof null. It is also a good lead-in to deeper questions about type coercion, equality, and how references work.

Finished reading?

Mark this topic as solved to track your progress.