Skip to content
blog
2 min read

Basic Data Types

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.

The Seven Primitive Types

String

Text, wrapped in quotes:

const name = "Reza";
const greeting = "Hello!";
const template = `Hi, ${name}`; // template literal

Number

All 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).

Boolean

true or false. The result of comparisons and logical operations:

const isActive = true;
const isOver18 = age >= 18;

Null

Intentional absence of a value:

const user = null; // no user yet

Undefined

Automatic absence — JavaScript assigned this, not you:

let name; // undefined
function f() {} // returns undefined

Symbol

A unique, immutable identifier. Rarely used in day-to-day code:

const id = Symbol("id");

BigInt

For integers too large for Number to represent accurately:

const big = 9007199254740993n;

Checking Types

Use typeof:

typeof "hello"; // 'string'
typeof 42; // 'number'
typeof true; // 'boolean'
typeof undefined; // 'undefined'
typeof null; // 'object' — historical bug, not actually an object
typeof Symbol(); // 'symbol'
typeof 9n; // 'bigint'

Type Coercion

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 numbers

The + operator prefers strings. Arithmetic operators prefer numbers. Use === to avoid coercion in comparisons.