JavaScript Data Types
string, number, boolean, null, undefined, symbol, bigint, object, typeof operator, type coercion
JavaScript Data Types
Every value in JavaScript has a type. Understanding JavaScript's data types is essential because the type of a value determines what operations you can perform on it and how it behaves in expressions. JavaScript is a dynamically typed language, meaning variables can hold any type without being declared as a specific type.
Primitive Types
JavaScript has seven primitive types. A primitive is an immutable value — when you manipulate it, you get a new value rather than modifying the original.
A string is a sequence of characters enclosed in single quotes, double quotes, or backticks. Use backticks for template literals that embed expressions.
const firstName = 'Alice';
const greeting = `Hello, ${firstName}!`;
console.log(greeting); // 'Hello, Alice!'A number in JavaScript represents all numeric values, including integers and decimals. There is no separate integer type.
const age = 30;
const price = 9.99;
const negative = -42;
console.log(Number.isInteger(age)); // trueA boolean is either true or false. Booleans are produced by comparisons and used in conditional logic.
null is an intentional absence of value. You assign it explicitly to indicate that a variable intentionally holds no value. undefined means a variable has been declared but not yet assigned a value.
let selectedItem = null; // no item chosen yet
let inputValue; // undefined — declared but not assigned
console.log(typeof null); // 'object' (historic quirk)
console.log(typeof undefined); // 'undefined'The typeof Operator
Use typeof to check a value's type at runtime. This is especially useful when handling dynamic data.
console.log(typeof 'hello'); // 'string'
console.log(typeof 42); // 'number'
console.log(typeof true); // 'boolean'
console.log(typeof undefined); // 'undefined'
console.log(typeof {}); // 'object'
console.log(typeof []); // 'object' (arrays are objects)
console.log(typeof function(){}); // 'function'Type Coercion
JavaScript automatically converts types in certain situations, a process called implicit coercion. This can produce surprising results. Always use === (strict equality) instead of == (loose equality) to avoid coercion issues.
console.log('5' + 3); // '53' — number coerced to string
console.log('5' - 3); // 2 — string coerced to number
console.log('' == false); // true — loose equality with coercion
console.log('' === false); // false — strict equality, no coercion