TExcelTAD
Home › JavaScript › JS Data Types

JavaScript Data Types

Every value in JavaScript has a type. There are eight of them: seven primitive types for simple values, and object for everything structured — including arrays, functions and dates.

Type Example values typeof result
String "hello", 'a', `hi ${name}` "string"
Number 42, 3.14, -7, NaN, Infinity "number"
BigInt 9007199254740993n "bigint"
Boolean true, false "boolean"
Undefined undefined "undefined"
Null null "object" (a historic bug)
Symbol Symbol("id") "symbol"
Object { a: 1 }, [1, 2], new Date(0) "object" (functions: "function")
js · try it yourself
console.log(typeof "text");
console.log(typeof 3.5);
console.log(typeof 10n);
console.log(typeof false);
console.log(typeof undefined);
console.log(typeof null);
console.log(typeof Symbol("id"));
console.log(typeof { a: 1 }, typeof [1, 2], typeof function () {});
string
number
bigint
boolean
undefined
object
symbol
object object function

Dynamic typing

Variables don't have types — values do. The same variable can hold different types over time, and JavaScript converts between types automatically in many operations:

js · try it yourself
let data = 5;
console.log(data + 5);      // number + number
data = "5";
console.log(data + 5);      // string + number -> string
console.log(data * 2);      // * converts the string to a number
10
55
10

This flexibility is convenient but can surprise you; the Type Conversion chapter explains the rules.

Strings, numbers and booleans

  • Strings hold text. Use single quotes, double quotes, or backticks; backticks allow ${…} placeholders.
  • Numbers cover integers and decimals in one type (64-bit floating point). Very large integers need BigInt.
  • Booleans are true or false — the result of comparisons.
js · try it yourself
const product = "Desk lamp";
const price = 29.99;
const inStock = price < 50;
console.log(`${product}: ${price} (${inStock ? "affordable" : "pricey"})`);
console.log(2 ** 53 + 1);     // beyond safe integer range — wrong!
console.log(2n ** 53n + 1n);  // BigInt is exact
Desk lamp: 29.99 (affordable)
9007199254740992
9007199254740993n

undefined vs null

Both mean "no value", but they're used differently:

  • undefined — the value hasn't been set: an unassigned variable, a missing property, a function with no return.
  • null — the value has been intentionally set to nothing by the programmer.
js · try it yourself
let middleName;
const user = { name: "Kim", manager: null };
console.log(middleName);
console.log(user.manager);
console.log(user.phone);       // property doesn't exist
console.log(null == undefined, null === undefined);
undefined
null
undefined
true false

Primitives vs objects

Primitive values are immutable and are copied by value. Objects are copied by reference — two variables can point to the same object:

js · try it yourself
let a = "hi";
let b = a;
b = b + "!";
console.log(a, b);           // a is unaffected

const list1 = [1, 2];
const list2 = list1;          // same array, two names
list2.push(3);
console.log(list1, list2);
hi hi!
[ 1, 2, 3 ] [ 1, 2, 3 ]

Tip: typeof can't tell arrays from plain objects. Use Array.isArray(value) to check for arrays, and value === null to check for null.

Test yourself

What does typeof null return?

  1. "null"
  2. "undefined"
  3. "object"
  4. "none"

How many primitive types does JavaScript have?

  1. 5
  2. 6
  3. 7
  4. 8

What does typeof [1, 2, 3] return?

  1. "array"
  2. "object"
  3. "list"
  4. "number"