JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicsjavascriptData Types
PrevNext
javascript
beginner
7 min read

Data Types

bigint
data-types
fundamentals
null
primitives
symbol
typeof
undefined

JavaScript has 7 primitive types (string, number, bigint, boolean, undefined, null, symbol) that are immutable and passed by value, plus Object as the non-primitive reference type.

Key Points

17 Primitive Types

string, number, bigint, boolean, undefined, null, symbol — immutable values compared and passed by value

2Object (Reference Type)

Objects, arrays, functions are mutable reference types — compared by reference, not value ({} === {} is false)

3typeof Operator

Returns the type as a string; typeof null is 'object' (historical bug); use === null to check for null

4null vs undefined

undefined is the engine's default for unassigned variables; null is the developer's explicit 'no value' — both are falsy

5Special Values

NaN !== NaN (use Number.isNaN()); BigInt cannot mix with Number without conversion; Symbol() is always unique

What You'll Learn

  • Understand the 7 primitive data types and how they differ from objects
  • Know the typeof operator quirks including typeof null
  • Understand the difference between null and undefined

Deep Dive

JavaScript is a dynamically typed language — variables don't have fixed types and can hold any value. The language has seven primitive types and one non-primitive type (Object). Understanding the distinction is fundamental to avoiding bugs and answering interview questions correctly.

The 7 Primitive Types

  • string: Textual data enclosed in single quotes, double quotes, or backticks. Strings are immutable — methods like .toUpperCase() return new strings.
  • number: 64-bit IEEE 754 floating-point. Handles both integers and decimals. Has special values: Infinity, -Infinity, and NaN (Not a Number). NaN !== NaN is true — use Number.isNaN() to check.
  • bigint: Arbitrary-precision integers for values beyond Number.MAX_SAFE_INTEGER (2^53 - 1). Created by appending n to a literal: 123n. Cannot be mixed with regular numbers in arithmetic without explicit conversion.
  • boolean: true or false. Used in conditionals and logical operations.
  • undefined: Automatically assigned to variables that are declared but not initialized, and to function parameters that weren't passed.
  • null: Intentional absence of a value. Unlike undefined, null is explicitly assigned by the developer.
  • symbol: Unique, immutable identifiers created with Symbol(). Every Symbol() call creates a globally unique value. Used as object property keys to avoid naming collisions.

Primitive vs Non-Primitive

  • Primitives are immutable — you cannot change the value itself, only reassign the variable. They are compared by value: 'hello' === 'hello' is true.
  • Objects (including arrays, functions, dates, and regex) are reference types. They are mutable and compared by reference: {} === {} is false because they are two different objects in memory.
  • Primitives are passed by value (a copy is made). Objects are passed by reference (the reference is copied, but both point to the same object).

The typeof Operator

  • typeof 'hello' → 'string', typeof 42 → 'number', typeof true → 'boolean', typeof undefined → 'undefined', typeof Symbol() → 'symbol', typeof 42n → 'bigint'.
  • typeof null → 'object' — this is a well-known bug from JavaScript's first implementation (1995) kept for backwards compatibility. Always check for null with === null.
  • typeof function(){} → 'function' — functions are objects, but typeof gives them a special return value.
  • typeof [] → 'object' — arrays are objects. Use Array.isArray() to check for arrays.

Key Interview Distinction: null vs undefined undefined means a variable exists but has no value assigned (the engine's default). null means a value was explicitly set to "nothing" (the developer's intent). Both are falsy. null == undefined is true (loose equality), but null === undefined is false (strict equality). Interviewers frequently test whether candidates understand this semantic difference.

Fun Fact

typeof null returning 'object' is a bug from 1995: in the original C implementation, null's type tag was 0 which matched the tag for objects — it was never fixed because too much code depended on it

Continue Learning

Type Coercion

beginner

Practice What You Learned

What are the primitive data types in JavaScript?
junior
data-types
JavaScript has 7 primitive data types: string, number, bigint, boolean, undefined, null, and symbol.
Previous
Data Structures
Next
Destructuring
PrevNext