Notes – JavaScript Data Types

In JavaScript, data types define the kind of values that can be stored and manipulated in a program. JavaScript is a dynamically typed language, meaning you donโ€™t need to declare variable types explicitly.


Categories of Data Types

JavaScript has two main categories of data types:

  1. Primitive Data Types (Immutable, stored directly in memory)
  2. Non-Primitive (Reference) Data Types (Stored by reference)

1. Primitive Data Types

These are the most basic data types in JavaScript. They hold a single value and are immutable.

Data TypeDescriptionExample
NumberRepresents integers & floating-point numberslet age = 25;
StringRepresents text (sequence of characters)let name = "John";
BooleanRepresents true/false valueslet isStudent = false;
UndefinedA variable declared but not assigned any valuelet x;
NullRepresents an intentional empty valuelet y = null;
Symbol (ES6)Unique and immutable identifierlet sym = Symbol("id");
BigInt (ES11)Used for very large numbers beyond Number limitslet big = 12345678901234567890n;

Example of Primitive Data Types

let num = 10;          // Number
let text = "Hello"; // String
let isValid = true; // Boolean
let value; // Undefined
let empty = null; // Null
let uniqueID = Symbol('id'); // Symbol
let bigNum = 12345678901234567890n; // BigInt

2. Non-Primitive (Reference) Data Types

These data types store complex structures and are mutable.

Data TypeDescriptionExample
ObjectCollection of key-value pairslet person = { name: "John", age: 30 };
ArrayOrdered list of valueslet numbers = [10, 20, 30];
FunctionA reusable block of codefunction greet() { console.log("Hi"); }

Example of Non-Primitive Data Types

let user = { name: "Alice", age: 25 }; // Object
let colors = ["red", "green", "blue"]; // Array
function sayHello() { console.log("Hello!"); } // Function

Checking Data Types

You can check the data type of a variable using typeof.

console.log(typeof 10);        // "number"
console.log(typeof "Hello"); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" (JavaScript quirk!)
console.log(typeof {}); // "object"
console.log(typeof []); // "object"
console.log(typeof function(){}); // "function"

Note: typeof null returns "object" due to a JavaScript bug that has existed since its early days.


Difference Between Primitive & Non-Primitive Data Types

FeaturePrimitive Data TypesNon-Primitive Data Types
StorageStored directly in memoryStored as a reference
MutabilityImmutable (cannot be changed)Mutable (values can be modified)
Copy BehaviorCreates a copy of the valueCreates a reference (changes affect the original)

Example: Primitive vs. Non-Primitive Copy Behavior

Primitive Copy

let a = 10;
let b = a;
b = 20;
console.log(a); // 10 (original remains unchanged)
console.log(b); // 20 (new copy modified)

Non-Primitive Copy

let obj1 = { name: "Alice" };
let obj2 = obj1;
obj2.name = "Bob";
console.log(obj1.name); // "Bob" (Both references point to the same object)