Basic Data Types
Explore JavaScript's basic data types: String, Number, Boolean, Null, Undefined, Symbol, and BigInt. Learn how they impact values and operations.
Explore JavaScript's basic data types: String, Number, Boolean, Null, Undefined, Symbol, and BigInt. Learn how they impact values and operations.
Every value in JavaScript has a type. Understanding types helps you predict how values behave in operations and comparisons.
Text, wrapped in quotes:
const name = "Reza";const greeting = "Hello!";const template = `Hi, ${name}`; // template literalAll numbers — integers and decimals — are the same type:
const age = 28;const price = 9.99;const negative = -5;Special number values: Infinity, -Infinity, and NaN (Not a Number).
true or false. The result of comparisons and logical operations:
const isActive = true;const isOver18 = age >= 18;Intentional absence of a value:
const user = null; // no user yetAutomatic absence — JavaScript assigned this, not you:
let name; // undefinedfunction f() {} // returns undefinedA unique, immutable identifier. Rarely used in day-to-day code:
const id = Symbol("id");For integers too large for Number to represent accurately:
const big = 9007199254740993n;Use typeof:
typeof "hello"; // 'string'typeof 42; // 'number'typeof true; // 'boolean'typeof undefined; // 'undefined'typeof null; // 'object' — historical bug, not actually an objecttypeof Symbol(); // 'symbol'typeof 9n; // 'bigint'JavaScript sometimes converts types automatically. This is called coercion:
"5" + 3; // '53' — number coerced to string"5" - 3; // 2 — string coerced to number"5" * "3"; // 15 — both coerced to numbersThe + operator prefers strings. Arithmetic operators prefer numbers. Use === to avoid coercion in comparisons.