Q1 · Types and coercion
What types does JavaScript have?
JavaScript has seven primitive types: string, number, bigint, boolean, undefined, symbol and null. Everything else is an object, including arrays, functions, dates and regular expressions. A variable is not typed; the value is. The same variable can hold a number one moment and a string the next.
In simple words: The value has a type, the variable does not; there are seven primitives, and everything else is an object.
Why it works
JavaScript is dynamically typed: you do not declare that price is a number. You just put a number in it. The engine tags each value with its type at run time. A primitive is a single value that is not an object: text, a number, a boolean, and so on. When you pass a primitive into a function or assign it, you copy the value itself. When you pass an object, you copy the reference, so two variables can point at the same object.
Functions are objects that you can call. Arrays are objects that use integer keys and a length. This is why typeof is not enough on its own: typeof [] and typeof {} are both "object", and typeof null is also "object" (Q2).
symbol values are unique tokens, often used as hidden keys. bigint holds integers that are too large for number (Q7). Both were added after the original language and have their own typeof results.
const values = [42, "chai", true, undefined, null, 10n, Symbol("id"),
{ city: "Pune" }, [1, 2], function () {}];
for (const value of values) {
console.log(typeof value);
}Output, real run on Node.js 26.13.0:
number
string
boolean
undefined
object
bigint
symbol
object
object
functionCommon mistakes
- Saying JavaScript has no types. It has types; it just does not bind them to variables.
- Calling arrays a separate type. They are objects (Array.isArray is the right test).
- Forgetting bigint and symbol when listing primitives.
Follow-ups
- Is a function a type? typeof fn is "function", which is a historical special case. A function is still an object: it can have properties.
- What about NaN? It is a number, not a separate type (Q5).
- What is TypeScript, then? (Advanced) TypeScript adds a type checker on top of JavaScript. The types are erased when the code is compiled, so the engine still only sees plain JavaScript at run time.
