Skip to main content

Data types

JavaScript values fall broadly into two categories: "primitive types" and "reference types."

Primitive types (basic types)

There are 7 primitive types in total, and they represent simple values themselves.

  1. Number

    let num = 42; // Integer
    let float = 3.14; // Decimal
    let infinity = Infinity; // Infinity
    let notANumber = NaN; // Not a Number
  2. String

    let str1 = "A double-quoted string";
    let str2 = 'A single-quoted string';
    let str3 = `Template literal: ${num}`; // You can embed variables
  3. Boolean

    let isTrue = true;
    let isFalse = false;
  4. undefined — a state where no value has been assigned yet

    let undefinedVar;
    console.log(undefinedVar); // undefined
  5. null — intentionally represents the absence of a value

    let emptyValue = null;
  6. Symbol — a unique value added in ES6

    let uniqueKey = Symbol('description');
  7. BigInt — a type for handling large integers

    let bigNumber = 1234567890123456789012345678901234567890n;
The difference between undefined and null

undefined is the state of "no value has been set yet (the system sets it automatically)," while null is the state of "the developer intentionally indicates the absence of a value." Both mean "no value," but they are used in different situations.

Reference types (object types)

Reference types can hold multiple values or more complex data structures.

  1. Object

    let person = {
    name: "Tanaka",
    age: 30,
    isStudent: false
    };
  2. Array

    let fruits = ["Apple", "Banana", "Orange"];
  3. Function

    function greet() {
    return "Hello!";
    }
Beginner-friendly explanation

Primitive types represent simple values themselves, while reference types let you handle multiple values together. There's also this difference: when you copy a primitive type, the value itself is duplicated, but when you copy a reference type, a "reference pointing to the same entity" is shared.

How to check a type

Using the typeof operator, you can check a value's type as a string.

console.log(typeof 42); // number
console.log(typeof "hello"); // string
console.log(typeof true); // boolean
console.log(typeof undefined); // undefined
console.log(typeof null); // object (a historical bug; see below)
console.log(typeof {}); // object
console.log(typeof []); // object (an array is also a kind of object)
console.log(typeof function(){}); // function

Checking for null and arrays correctly

Because typeof returns "object" for both null and arrays, you use other methods to check for them.

// Check for null with ===
const value = null;
console.log(value === null); // true

// Check for an array with Array.isArray()
console.log(Array.isArray([1, 2, 3])); // true
console.log(Array.isArray({})); // false
Why typeof null is "object"

This is a historical bug that originates from JavaScript's early implementation. The internal representation of a value carries a tag indicating its type; the tag for objects was 0, and null was also represented as 0, so it gets judged as an "object." A huge amount of code already depends on this behavior, so it has been left unchanged for backward compatibility. Use value === null to check for null.

  • Conditionals — Branch your logic using values of each data type