JavaScript Data Types
Primitive, Reference, Dynamic Typing
Introduction to JavaScript Data Types
Every value in JavaScript belongs to a data type. Understanding these data types is essential to control behavior, write meaningful conditions, and build flexible functions. JavaScript's type system includes both primitive types and reference types.
1. Primitive Data Types
Primitive types are immutable and directly represent a single value. JavaScript provides six primitive types:
- Number
- String
- Boolean
- Undefined
- Null
- Symbol (introduced in ES6)
- BigInt (introduced in ES11/ES2020)
Number
let score = 99.5;
let count = 10;
console.log("typeof score =>", typeof score);
console.log("typeof count =>", typeof count);
typeof score => number
typeof count => number
JavaScript uses a single type for both integers and floating-point numbers.
String
let name = "Alice";
let greeting = 'Hello';
let message = `Welcome, ${name}`;
console.log("typeof name =>", typeof name);
console.log("typeof message =>", typeof message);
typeof name => string
typeof message => string
Boolean
let isLoggedIn = true;
let isAdmin = false;
console.log("typeof isLoggedIn =>", typeof isLoggedIn);
typeof isLoggedIn => boolean
Undefined
let user;
console.log(user);
undefined
Any variable declared but not initialized is undefined
.
Null
let session = null;
console.log("typeof session =>", typeof session); // This is a known JavaScript quirk
typeof session => object
Symbol (ES6)
let sym = Symbol('id');
console.log("typeof sym =>", typeof sym);
typeof sym => symbol
Symbols are unique and immutable, often used for unique keys in objects.
BigInt (ES2020)
let bigNumber = 9007199254740991n;
console.log("typeof bigNumber =>", typeof bigNumber);
typeof bigNumber => bigint
2. Reference Data Types
Unlike primitive values, reference types store memory addresses, not actual values. These include:
- Objects
- Arrays
- Functions
Object
let user = {
name: "Alice",
age: 25
};
console.log(typeof user);
object
Array
let colors = ["red", "green", "blue"];
console.log("typeof colors =>", typeof colors);
console.log("Array.isArray(colors) =>", Array.isArray(colors));
typeof colors => object
Array.isArray(colors) => true
Function
function greet() {
return "Hi there!";
}
console.log(typeof greet); // 'function'
function
3. JavaScript is Dynamically Typed
JavaScript doesn’t require variable types to be declared explicitly. Types are assigned at runtime:
let dynamic = 5; // number
console.log(typeof dynamic); // "number"
dynamic = "now a string"; // string
console.log(typeof dynamic); // "string"
dynamic = true; // boolean
console.log(typeof dynamic); // "boolean"
number
string
boolean
4. Type Checking with typeof
The typeof
operator helps identify the data type of a variable:
console.log(typeof 42); // "number"
console.log(typeof 'hello'); // "string"
console.log(typeof null); // "object" (legacy bug)
console.log(typeof undefined); // "undefined"
console.log(typeof Symbol()); // "symbol"
number
string
object
undefined
symbol
5. Comparing null
and undefined
While both represent empty values, their meanings differ:
let a; // undefined
let b = null; // null
console.log(a == b); // true (type coercion)
console.log(a === b); // false (strict comparison)
true
false
6. Special Case: NaN
NaN
stands for “Not a Number”. It is returned when a mathematical operation fails:
let invalid = "abc" / 2;
console.log(invalid); // NaN
console.log(typeof invalid); // 'number'
NaN
number
7. Wrapper Objects (Boxing)
Primitive values can temporarily behave like objects. JavaScript automatically wraps them in object wrappers:
let str = "hello";
console.log(str.toUpperCase()); // "HELLO"
HELLO
Behind the scenes, JavaScript uses new String(str)
to allow method calls.
8. Type Conversion (Coercion)
Implicit Conversion
console.log("5" + 1); // "51"
console.log("5" - 1); // 4
console.log(true + 1); // 2
51
4
2
Explicit Conversion
console.log(Number("5")); // 5
console.log(String(100)); // "100"
console.log(Boolean(0)); // false
5
100
false
Summary Table: Data Types Overview
Type | Category | Example |
---|---|---|
Number | Primitive | 42 |
String | Primitive | "JavaScript" |
Boolean | Primitive | true |
Null | Primitive | null |
Undefined | Primitive | let a; |
Symbol | Primitive | Symbol("id") |
BigInt | Primitive | 12345678901234567890n |
Object | Reference | { name: "Alice" } |
Array | Reference | [1, 2, 3] |
Function | Reference | function() {} |
Conclusion
JavaScript's data types form the foundation of every program. By understanding both primitive and reference types, dynamic typing, and the quirks of type coercion, you gain the power to write cleaner, more predictable code. This knowledge also lays the groundwork for mastering scopes, closures, object-oriented JS, and advanced data structures.
Comments
Loading comments...