🌱BeginnerJavaScript Fundamentals

Data Types

The seven primitive types and one object type that every JavaScript value belongs to.

Overview

Every value in JavaScript has a type. There are seven primitive types: string, number, bigint, boolean, undefined, null, and symbol. Everything else arrays, functions, dates, regular expressions is an object. Primitives are immutable values. When you write const name = "Alice", the string "Alice" cannot be changed in-place. Operations on primitives create new values rather than modifying the original. The typeof operator returns a string describing the type of any value. It is your go-to tool for checking types at runtime. One famous quirk: typeof null returns "object" this is a 40-year-old bug in the language that can never be fixed without breaking the web. JavaScript's type coercion the automatic conversion between types in certain operations is both a feature and a notorious source of bugs. Understanding the type system well helps you predict and avoid coercion surprises.

Key Points

  • •Seven primitives: string, number, bigint, boolean, undefined, null, symbol
  • •typeof checks a value's type at runtime and returns a string
  • •undefined means a variable has been declared but not assigned a value
  • •null is an explicit "no value" use it to intentionally empty a variable
  • •NaN (Not a Number) is paradoxically of type "number"; check for it with Number.isNaN()
  • •typeof null === "object" is a historical bug always check null with === null

Examples

The seven primitive types

console.log(typeof "hello");      // string
console.log(typeof 42);           // number
console.log(typeof 9007199254740993n); // bigint
console.log(typeof true);         // boolean
console.log(typeof undefined);    // undefined
console.log(typeof null);         // object  ← famous bug!
console.log(typeof Symbol("id")); // symbol

undefined vs null the difference

let age;
console.log(age);           // undefined declared, not assigned

let address = null;
console.log(address);       // null explicitly "no value"

console.log(age === null);        // false
console.log(address === null);    // true

Number quirks: NaN and Infinity

console.log(0 / 0);          // NaN
console.log(1 / 0);          // Infinity
console.log(typeof NaN);     // "number"  ← surprising
console.log(NaN === NaN);    // false     ← NaN is not equal to itself
console.log(Number.isNaN(NaN)); // true  ← correct way to check

Common Mistakes

  • Using == instead of === loose equality does type coercion: "5" == 5 is true but "5" === 5 is false
  • Checking typeof null === "null" it returns "object", so use value === null instead
  • Treating NaN === NaN as true always use Number.isNaN() to check for NaN

Note

Always use === (strict equality) instead of == (loose equality) to avoid unexpected type coercion. The only common exception: someValue == null checks for both null and undefined at once.

Exercises

Write JavaScript code to solve each exercise. Run it and check your output.

Loading editor…

Completed: 0 / 5 exercises

Knowledge Check

Question 1 of 4

How many primitive types does JavaScript have?