CPCODELAB
JavaScript

Data Types

10 min read

JavaScript's Eight Data Types

JavaScript is dynamically typed: variables do not have a fixed type, values do. There are seven primitive types and one object type.

  • number — integers and floats (64-bit IEEE 754)
  • string — text, always immutable
  • booleantrue or false
  • undefined — a variable that has been declared but not assigned
  • null — an intentional absence of a value
  • bigint — arbitrary-precision integers, e.g. 9007199254740993n
  • symbol — guaranteed-unique identifiers
  • object — everything else: plain objects, arrays, functions, dates, …
javascript
console.log(typeof 42);          // "number"
console.log(typeof "hello");     // "string"
console.log(typeof true);        // "boolean"
console.log(typeof undefined);   // "undefined"
console.log(typeof null);        // "object" (historical bug!)
console.log(typeof {});          // "object"
console.log(typeof []);          // "object"
console.log(typeof function(){}); // "function"

typeof null === "object" is a well-known quirk that dates back to JavaScript 1.0 and cannot be fixed without breaking the web. To check for null, use strict equality: value === null.

Primitives vs Objects

Primitives are immutable and compared by value. Objects are mutable and compared by reference — two objects with the same content are not equal unless they are the same object in memory.

javascript
const a = { x: 1 };
const b = { x: 1 };
console.log(a === b); // false — different references

const c = a;
console.log(a === c); // true — same reference
Ready to test yourself?
Practice JavaScript— quiz & coding exercises