← Back to Chapters

JavaScript Data Types

? JavaScript Data Types

? Quick Overview

JavaScript provides different data types to store and work with values. These are grouped into two main categories:

  • Primitive data types: string, number, bigint, boolean, undefined, symbol, null
  • Non-primitive (reference) data types: object (including arrays, functions, and other complex structures)

Understanding data types is essential for storing values correctly, avoiding bugs, and using memory efficiently.

? Key Concepts

  • Primitive values are simple, immutable values stored directly (e.g., numbers, strings, booleans).
  • Reference values (objects, arrays, functions) are stored by reference, meaning variables point to a location in memory.
  • The typeof operator helps you check the data type of a value at runtime.
  • null is a special value that represents “no value” or “empty”, but typeof null returns "object" due to a historical bug in JavaScript.
  • Arrays and functions are specialized kinds of objects in JavaScript.

? Syntax and Theory

You can declare variables using let, const, or var (modern code usually prefers let and const):

  • Strings store text inside quotes: "Hello" or 'Hello'.
  • Numbers store numeric values: integers, decimals, etc.
  • BigInt is used for very large integers and is written with an n at the end.
  • Boolean stores true or false.
  • Undefined means a variable has been declared but not assigned a value.
  • Null is an intentional “empty” value set by the programmer.
  • Symbol creates unique identifiers, often used in advanced scenarios.
  • Objects store key–value pairs; arrays and functions are special kinds of objects.

? Code Examples

? Primitive Data Types

? View Primitive Data Types Example
// String
let name = "Alice";

// Number
let age = 25;

// BigInt
let bigNumber = 1234567890123456789012345678901234567890n;

// Boolean
let isStudent = true;

// Undefined
let score;
console.log(score); // undefined

// Null
let data = null;

// Symbol
let id = Symbol("unique");

? Reference (Non-Primitive) Data Types

? View Object, Array and Function Example
// Object
let person = {
  name: "John",
  age: 30
};

// Array
let colors = ["red", "green", "blue"];

// Function
function greet() {
  return "Hello!";
}

? typeof Operator

The typeof operator returns a string describing the data type of a value.

? View typeof Example
console.log(typeof "hello");      // "string"
console.log(typeof 123);          // "number"
console.log(typeof true);         // "boolean"
console.log(typeof undefined);    // "undefined"
console.log(typeof null);         // "object" (historical bug)
console.log(typeof []);           // "object"
console.log(typeof {});           // "object"
console.log(typeof function(){}); // "function"

?️ Live Output and Explanation

If you run the typeof example in the browser console or a JS environment, you will see:

  • typeof "hello""string"
  • typeof 123"number"
  • typeof true"boolean"
  • typeof undefined"undefined"
  • typeof null"object" (this is a long-known quirk in JavaScript)
  • typeof [] and typeof {}"object" (arrays are objects)
  • typeof function(){}"function" (a special subtype of object)

Similarly, in the primitive example, logging score before assigning a value prints undefined, which means the variable exists but does not hold any value yet.

? Tips and Best Practices

  • Use typeof to quickly check the type of a variable while debugging.
  • Remember that null is an “empty” value but typeof null returns "object" due to a historical bug.
  • Arrays and functions are objects; use Array.isArray() instead of typeof to detect arrays.
  • Prefer const for values that do not change and let for values that will change.
  • Be mindful of primitives (stored by value) vs objects (stored by reference) when copying or comparing variables.

? Try It Yourself

  • Declare variables using all primitive data types and log both their values and their typeof results.
  • Create an object and an array, then access and log their properties/elements.
  • Use typeof on a function, array, object, number, and null. Observe the results.
  • Use Array.isArray() to check whether a variable is an array and compare it to using typeof.