Data Types

Easy

JavaScript has 7 primitive types and 1 object type. Primitives are immutable values stored by value, while objects are mutable and stored by reference. Knowing your types prevents bugs!

Interactive Visualization

Key Points

  • 7 primitives: string, number, boolean, null, undefined, symbol, bigint
  • Objects: everything else (arrays, functions, dates, etc.)
  • typeof returns the type as a string
  • Primitives are copied by value, objects by reference
  • null and undefined both mean "no value" but are different

Code Examples

Primitive Types

// String
let name = "Alice";

// Number (integers and floats)
let age = 25;
let price = 19.99;

// Boolean
let isActive = true;

// null (intentionally empty)
let data = null;

// undefined (not yet assigned)
let x;
console.log(x); // undefined

The 5 most common primitive types

typeof Operator

typeof "hello"    // "string"
typeof 42         // "number"
typeof true       // "boolean"
typeof undefined  // "undefined"
typeof null       // "object" (bug!)
typeof {}         // "object"
typeof []         // "object"
typeof function(){} // "function"

typeof null being "object" is a famous JavaScript bug

Value vs Reference

// Primitives: copied by value
let a = 10;
let b = a;
b = 20;
console.log(a); // 10 (unchanged!)

// Objects: copied by reference
let obj1 = { x: 10 };
let obj2 = obj1;
obj2.x = 20;
console.log(obj1.x); // 20 (changed!)

Changing obj2 affects obj1 because they point to the same object

Common Mistakes

  • Forgetting typeof null returns "object"
  • Comparing objects with === (compares references, not values)
  • Confusing null and undefined

Interview Tips

  • List all 7 primitives confidently
  • Explain the typeof null quirk
  • Show value vs reference with a simple example

Related Concepts