⬅ Previous Topic
JavaScript VariablesNext Topic ⮕
JavaScript Type Conversion⬅ Previous Topic
JavaScript VariablesNext Topic ⮕
JavaScript Type ConversionEvery 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.
Primitive types are immutable and directly represent a single value. JavaScript provides six primitive types:
let score = 99.5;
let count = 10;
typeof score => 'number'
typeof count => 'number'
JavaScript uses a single type for both integers and floating-point numbers.
let name = "Alice";
let greeting = 'Hello';
let message = `Welcome, ${name}`;
typeof name => 'string'
typeof message => 'string'
let isLoggedIn = true;
let isAdmin = false;
typeof isLoggedIn => 'boolean'
let user;
console.log(user);
undefined
Any variable declared but not initialized is undefined
.
let session = null;
typeof session => 'object' // This is a known JavaScript quirk
let sym = Symbol('id');
typeof sym => 'symbol'
Symbols are unique and immutable, often used for unique keys in objects.
let bigNumber = 9007199254740991n;
typeof bigNumber => 'bigint'
Unlike primitive values, reference types store memory addresses, not actual values. These include:
let user = {
name: "Alice",
age: 25
};
typeof user => 'object'
let colors = ["red", "green", "blue"];
typeof colors => 'object'
Array.isArray(colors) => true
function greet() {
return "Hi there!";
}
typeof greet => 'function'
JavaScript doesn’t require variable types to be declared explicitly. Types are assigned at runtime:
let dynamic = 5; // number
dynamic = "now a string"; // string
dynamic = true; // boolean
typeof dynamic => changes at runtime
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'
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)
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'
Primitive values can temporarily behave like objects. JavaScript automatically wraps them in object wrappers:
let str = "hello";
console.log(str.toUpperCase()); // "HELLO"
Behind the scenes, JavaScript uses new String(str)
to allow method calls.
console.log("5" + 1); // "51"
console.log("5" - 1); // 4
console.log(true + 1); // 2
console.log(Number("5")); // 5
console.log(String(100)); // "100"
console.log(Boolean(0)); // false
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() {} |
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.
⬅ Previous Topic
JavaScript VariablesNext Topic ⮕
JavaScript Type ConversionYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.