JavaScript · Getting Started

Data Types

Every value in JavaScript has a type. Knowing the types — and the quirks of typeof — helps you avoid confusing bugs.

Beginner 5 min Lesson 4 of 49 Runnable examples
0/49 in JavaScript
On this page · 5 min · Beginner

Learning objectives#

  • List the seven primitive types
  • Distinguish primitives from objects
  • Use typeof and know its two famous quirks
  • Check for arrays with Array.isArray()

Explanation#

JavaScript has seven primitive types: string, number, bigint, boolean, undefined, null and symbol. Everything else — arrays, functions, dates, plain objects — is an object.

Primitives are immutable and compared by value. Objects are compared by reference: two separate objects with the same contents are not equal.

typeof returns a string naming the type, with two historical quirks: typeof null is "object", and typeof [] is "object" because arrays are objects. Use Array.isArray() to detect arrays.

Code example: Inspecting types#

JavaScriptInspecting types Runnable
console.log(typeof 'hello');      // string
console.log(typeof 42);           // number
console.log(typeof 10n);          // bigint
console.log(typeof true);         // boolean
console.log(typeof undefined);    // undefined
console.log(typeof Symbol('id')); // symbol
console.log(typeof null);         // object  (historical bug)
console.log(typeof []);           // object  (arrays are objects)
console.log(typeof function(){}); // function
console.log(Array.isArray([]));   // true
Expected output
string
number
bigint
boolean
undefined
symbol
object
object
function
true
Opens this example in the full playground

Step-by-step explanation#

  1. Each line passes a value to typeof and prints the resulting string.
  2. typeof null returning "object" is a bug from the first version of JavaScript that cannot be fixed without breaking the web.
  3. Arrays are specialised objects, so typeof reports "object".
  4. Functions are objects too, but typeof gives them their own "function" result.
  5. Array.isArray() is the reliable way to check for an array.

Real-world use case#

Validating API responses, writing utility functions that accept different inputs, and debugging “undefined is not a function” errors all require a clear picture of types.

Common mistakes#

Checking for null with typeof

Use value === null instead of typeof value === "object".

Comparing objects with ===

{ a: 1 } === { a: 1 } is false because they are two different objects.

Best practices#

  • Use === and !== to avoid implicit type conversion
  • Use Array.isArray() for arrays and === null for null
  • Use Number.isNaN() rather than the global isNaN()
  • Consider TypeScript for compile-time type checking

Remember this#

Primitives: string, number, boolean, null, undefined, bigint, symbol.

Everything else is an object (arrays, functions too).

typeof null === "object" is a famous quirk.

Practice exercise#

Your task: Write a function describe(value) that returns "array", "null" or the typeof result for anything else.

Show solution
JavaScript Runnable
function describe(value) {
  if (Array.isArray(value)) return 'array';
  if (value === null) return 'null';
  return typeof value;
}
console.log(describe([1]), describe(null), describe('hi')); // array null string
Opens this example in the full playground

Mini quiz#

Data Types quizPredict the output Question 1 of 3

What is the output of this code?

console.log(typeof []);
Score so far: 0/0

Want more? Practise all JavaScript MCQs · JavaScript interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.