What are the primitive data types in JavaScript?
JavaScript has seven primitive types: string, number, bigint, boolean, undefined, null and symbol. Everything else, including arrays and functions, is an object.
Explain it simply
Think of primitives as single coins in your pocket: a coin is just a value, you cannot open it up and change what is inside. Objects are more like a wallet: one container that can hold many things and can be changed.
The seven primitives are:
- string: text, like
"IRCTC" - number: integers and decimals share one type, like
42or3.14 - bigint: whole numbers bigger than
Number.MAX_SAFE_INTEGER, written with ann, like10n - boolean:
trueorfalse - undefined: a variable that has been declared but given no value
- null: a value you set on purpose to mean "nothing here"
- symbol: a unique identifier, mostly used as object keys
Primitives are immutable. When you "change" a string, JavaScript actually builds a new string.
Example
console.log(typeof "IRCTC"); // Output: string
console.log(typeof 42); // Output: number
console.log(typeof 10n); // Output: bigint
console.log(typeof true); // Output: boolean
console.log(typeof undefined); // Output: undefined
console.log(typeof Symbol("id")); // Output: symbol
console.log(typeof null); // Output: object
console.log(typeof [1, 2]); // Output: objectWhat to say in the interview
"There are seven primitives: string, number, bigint, boolean, undefined, null and symbol. They are immutable and compared by value. Everything else is an object, including arrays and functions, and objects are compared by reference. One quirk: typeof null returns 'object', which is a historical bug kept for backward compatibility."
Common mistakes
- Saying there are separate integer and float types. JavaScript has one number type (64-bit floating point), plus bigint.
- Forgetting symbol and bigint, which were added in ES6 and ES2020.