Primitive Data Types in JavaScript
In JavaScript, data types categorize the kinds of values a variable can hold, determining how the engine stores and operates on them. This article provides a clear, comprehensive breakdown of all seven primitive data types in JavaScript: String, Number, BigInt, Boolean, Undefined, Null, and Symbol. You will learn what each data type represents, how it is used, and the fundamental properties that distinguish primitives from complex objects.
What Is a Primitive Data Type?
A primitive data type is data that is not an object and has no methods or properties of its own. In JavaScript, primitive values are immutable (their values cannot be altered once created) and are passed by value rather than by reference.
JavaScript has seven primitive data types:
1. String
A String represents textual data. Strings are sequences
of characters enclosed in single quotes ('), double quotes
("), or backticks (`) for template
literals.
let single = 'Hello';
let double = "World";
let template = `${single} ${double}!`; // Template literal2. Number
The Number type represents both integer and
floating-point values. In JavaScript, numbers are stored as 64-bit
floating-point values (IEEE 754 standard). This type also includes
special numeric values: Infinity, -Infinity,
and NaN (Not-a-Number).
let integer = 42;
let float = 3.14;
let invalid = "text" / 2; // NaN3. BigInt
Introduced in ES2020, BigInt allows the representation
of integers larger than \(2^{53} - 1\)
(the maximum safe integer for the Number type). A
BigInt is created by appending n to the end of
an integer literal or by calling the BigInt()
constructor.
let largeNumber = 9007199254740991n;
let anotherBigInt = BigInt(9007199254740991);4. Boolean
A Boolean represents a logical entity with only two
possible values: true and false. Booleans are
typically used in conditional statements and control flow.
let isLoggedIn = true;
let hasPermission = false;5. Undefined
A variable that has been declared but has not yet been assigned a
value is automatically assigned the type and value of
undefined. It signifies the unintentional absence of a
value.
let user;
console.log(user); // Output: undefined6. Null
The Null type has exactly one value: null.
It represents the intentional absence of any object value. Unlike
undefined, which indicates that a value has not been
initialized, null is explicitly assigned to indicate “no
value” or “empty.”
let selectedItem = null;Note: Calling typeof null returns
"object" due to a legacy design quirk in JavaScript, but
null is fundamentally a primitive.
7. Symbol
Introduced in ES6, a Symbol represents a unique and
immutable identifier. Symbols are often used to create private or
collision-free property keys for objects.
const id1 = Symbol('id');
const id2 = Symbol('id');
console.log(id1 === id2); // Output: falseKey Takeaways
- Immutability: When you perform operations on primitive values, JavaScript produces a new value rather than modifying the original.
- Storage: Primitives are stored directly in the location that the variable accesses (on the stack), making operations on them fast and memory-efficient.
- Type Checking: You can inspect the type of any
primitive using the
typeofoperator (e.g.,typeof "hello"returns"string").