Values and Types

JavaScript ES2023 · ✓ verified by execution on 2026-08-06

JavaScript has a number of built-in primitive data types. These values are immutable, meaning they cannot be altered once created.

Primitive Types

JavaScript Data Types Primitive Types (Immutable) string number boolean undefined null Object Types Object (Arrays, Functions, etc.)
JavaScript's type system: the immutable primitives (string, number, boolean, undefined, null, plus symbol and bigint) on one side, and object types such as arrays and functions on the other.

All types except Object define immutable values

javascript
console.log(typeof 42);
Output
number
javascript
console.log(typeof "hello");
Output
string
javascript
console.log(typeof true);
Output
boolean

Undefined vs Null

When a variable is declared without an explicit value, JavaScript automatically initializes it with the undefined primitive. This represents the default state of uninitialized data in the language.

javascript
let notAssigned;
console.log(typeof notAssigned);
Output
undefined

On the other hand, null is a distinct primitive that you must assign deliberately. It signifies the purposeful absence of an object or value.

javascript
console.log(null === undefined);
Output
false

The typeof null Bug

The typeof operator normally returns a string representing the primitive type of the given value. However, there is a very famous exception when checking null.

Predict the output javascript

Read the code. What exactly will it print? Commit to an answer before you look.

console.log(typeof null);
Output
object

Why This Bug Survives

You might wonder why typeof null returns "object" if null is actually a primitive type. "null" would be the sensible answer. What you get instead is a leak from the first implementation, in 1995, that has been too widely depended on to fix.

In that original engine, JavaScript values were stored in 32-bit chunks, where the lowest 3 bits represented a type tag. The tag for objects happened to be 000. Meanwhile, null was implemented as a traditional C-style NULL pointer, which consists of all zeros. Because its lowest 3 bits were also 000, the engine mistakenly read it as an object and returned "object".

The first version of the language was built in about ten days, and this is one of the seams that shows. A proposal to fix this bug and make it return "null" was actually drafted for ECMAScript 6, but it was ultimately rejected. The web relies on backward compatibility, and fixing it would have broken millions of older websites that unknowingly relied on the bug. So typeof cannot tell you whether something is null. Compare directly instead: value === null.

Object Value ... Pointer ... 000 Type Tag Null Pointer 000000000000... 000 Accidental Tag typeof returns "object"
The binary cause of the typeof null bug

Check yourself

undefined and null are exactly the same thing.

Reveal answer

They are different types. undefined means a variable has been declared but not assigned a value. null is an assignment value representing no object. — They are different types. undefined means a variable has been declared but not assigned a value. null is an assignment value representing no object.

Because typeof null is 'object', null must be an object.

Reveal answer

null is a primitive type. typeof null returning 'object' is a known, unfixable bug in the language for backward compatibility. — null is a primitive type. typeof null returning 'object' is a known, unfixable bug in the language for backward compatibility.

Everything in JavaScript is an object.

Reveal answer

No, primitive values like strings, numbers, and booleans are not objects, although they have object wrappers. — No, primitive values like strings, numbers, and booleans are not objects, although they have object wrappers.

typeof is a function and requires parentheses.

Reveal answer

typeof is an operator. You can write typeof 42 instead of typeof(42). — typeof is an operator. You can write typeof 42 instead of typeof(42).

Challenges

Challenge 1 +20 XP

Print the type of the value `null` using the `typeof` operator.

javascript
  • Test 1 — expects "object\n"
Need a hint? (−25% XP)

Use console.log() and the typeof operator with null.

Show solution (0 XP)
console.log(typeof null);

Challenge 2 +20 XP

Declare a variable named `mystery` without giving it a value. Then print its type.

javascript
  • Test 1 — expects "undefined\n"
Need a hint? (−25% XP)

Use 'let' to declare without assignment, then console.log the typeof the variable.

Show solution (0 XP)
let mystery;
console.log(typeof mystery);