Full answer
Primitive types are immutable values: string, number, bigint, boolean, undefined, symbol, and null. Everything else is an object (objects, arrays, functions, dates, maps, and so on), compared by reference.
typeof returns one of these strings: 'string', 'number', 'bigint', 'boolean', 'undefined', 'symbol', 'function', 'object'. The quirks:
typeof nullis'object'. This is a bug from the first version of the language that cannot be fixed without breaking the web. Test for null withvalue === null.typeof []is'object', and so are dates, regular expressions, and maps. UseArray.isArray(value)for arrays, andvalue instanceof DateorObject.prototype.toString.call(value)for others.typeof function () {}andtypeof class {}are'function'. Functions are objects with extra behavior, and'function'is a special case of the operator.typeof NaNis'number', because NaN is a special numeric value.typeof undeclaredNameis'undefined'and does not throw, while reading an undeclared name throws aReferenceError. That is whytypeof window !== 'undefined'is a safe environment check.- Variables in the temporal dead zone throw even with
typeof. typeof new String('a')is'object'. Wrapper objects created withnewdiffer from primitive strings. Avoid them.typeof 10nis'bigint', andtypeof Symbol()is'symbol'.
A reliable type check helper: Object.prototype.toString.call(value) returns strings such as '[object Array]', '[object Null]', and '[object Date]'.
Primitives have no properties of their own, but they get temporary wrapper objects when you call methods: 'abc'.toUpperCase() works because the engine wraps the string for the call.
const samples = [
['"text"', 'text'], ['42', 42], ['NaN', NaN], ['10n', 10n], ['true', true], ['undefined', undefined],
['Symbol()', Symbol('s')], ['null', null], ['{}', {}], ['[]', []], ['new Date()', new Date(0)],
['/x/', /x/], ['function', function () {}], ['class', class {}], ['() => {}', () => {}],
['new Map()', new Map()], ['new String("a")', new String('a')],
];
for (const [label, value] of samples) {
console.log(label.padEnd(18), typeof value, '|', Object.prototype.toString.call(value));
}
console.log('typeof undeclared:', typeof thisNameIsNotDeclared);
try {
thisNameIsNotDeclared;
} catch (error) {
console.log('reading it throws:', error.name);
}
try {
typeof later;
let later = 1;
} catch (error) {
console.log('typeof in the temporal dead zone throws:', error.name);
}
const isPlainObject = (value) => Object.prototype.toString.call(value) === '[object Object]';
console.log(isPlainObject({}), isPlainObject([]), isPlainObject(null), isPlainObject(new Map()));
console.log(Array.isArray([]), Array.isArray({ length: 0 }), null === null, typeof null === 'object');
const primitive = 'abc';
const wrapper = new String('abc');
console.log(primitive === 'abc', wrapper === 'abc', wrapper == 'abc', typeof primitive, typeof wrapper);
primitive.extra = 1;
console.log('properties on a primitive are lost:', primitive.extra);Run with Node.js v24.22.1 as js-152.js; two runs gave identical output. Output: "text" string | [object String] 42 number | [object Number] NaN number | [object Number] 10n bigint | [object BigInt] true boolean | [object Boolean] undefined undefined | [object Undefined] Symbol() symbol | [object Symbol] null object | [object Null] {} object | [object Object] [] object | [object Array] new Date() object | [object Date] /x/ object | [object RegExp] function function | [object Function] class function | [object Function] () => {} function | [object Function] new Map() object | [object Map] new String("a") object | [object String] typeof undeclared: undefined reading it throws: ReferenceError typeof in the temporal dead zone throws: ReferenceError true false false false true false true true true false true string object properties on a primitive are lost: undefined
Key points
- Seven primitives plus object; primitives are immutable and compared by value.
- typeof returns a string; null gives "object" (historic bug) so test with === null.
- Arrays, dates, and maps are "object"; use Array.isArray or toString.call for details.
- Functions give "function"; NaN gives "number"; an undeclared name gives "undefined" without throwing.
- Wrapper objects from new String and similar are objects; avoid them.
Common mistakes
- Using typeof to detect null or arrays.
- Using new String, new Number, or new Boolean.
- Reading an undeclared variable to test if it exists instead of using typeof.
- Assuming that functions are not objects.
Follow-ups
- Why does typeof null return object?
- How would you write a function that returns an accurate type name for any value?
- What happens to a property assigned on a primitive string?
Tips
- List the seven primitives from memory and say everything else is an object.
- Name the typeof null bug and the right replacement.
- Mention the safe typeof check for undeclared names.