JavaScript Data Types

The Kinds of Values JavaScript Knows About

JavaScript Data Types

A tour of JavaScript's data types — strings, numbers, booleans, objects, and the special ones (null, undefined, bigint, symbol).

5 min read Level 1/5 #data-types#typeof#primitives
What you'll learn
  • Identify the seven primitive types and one object type
  • Use `typeof` to check a value's type
  • Understand that variables aren't typed — values are

Every value in JavaScript has a type. The language has eight, split into seven primitives and one object type.

TypeExampleNotes
string"hello"Text, any length
number42, 3.14Integers and floats — same type
booleantrue, falseJust two values
nullnull”Intentionally no value”
undefinedundefined”No value assigned yet”
bigint9007199254740993nWhole numbers bigger than number can hold
symbolSymbol("id")Unique, advanced
object{ a: 1 }, [1, 2], function(){}Everything else: objects, arrays, functions, dates…

Checking a Type With typeof

The typeof operator returns a string naming the type.

typeof in action script.js
console.log(typeof "hello");   // "string"
console.log(typeof 42);        // "number"
console.log(typeof true);      // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof { a: 1 });  // "object"
console.log(typeof [1, 2, 3]); // "object"  ← arrays are objects
▶ Preview: console

Dynamic Typing

JavaScript variables don’t have types — values do. The same name can hold values of different types over time.

A variable can change type script.js
let value = 42;
console.log(typeof value); // "number"

value = "now I'm a string";
console.log(typeof value); // "string"

value = true;
console.log(typeof value); // "boolean"
▶ Preview: console

This is called dynamic typing. It makes JavaScript flexible but also makes some bugs harder to catch. If you find this brittle, TypeScript adds static types on top of JavaScript.

null vs undefined

Two values for “nothing”, with subtly different intent.

  • undefined — the language gives you this. A variable you declared but didn’t assign, a function parameter you didn’t pass, a property that doesn’t exist.
  • nullyou give this. It says “I deliberately wanted this to be empty.”
null vs undefined script.js
let a;
console.log(a); // undefined — not assigned yet

let b = null;
console.log(b); // null — intentionally empty

console.log(null === undefined);  // false  — different values
console.log(null == undefined);   // true   — loose equality says yes
▶ Preview: console

Up Next

The next handful of lessons drill into the most-used types — strings first.

JavaScript Strings →