← Back to JavaScript, page 8

javascript-0152 · Easy fresher

What are the data types in JavaScript, how does typeof work, and which typeof results are surprising?

JavaScript has seven primitive types (string, number, bigint, boolean, undefined, symbol, null) and one structural type, object, which includes arrays, functions, and dates. The typeof operator returns a string and is reliable for primitives except null, which gives "object" because of an old bug. Functions give "function" although they are objects, and arrays give "object", so use Array.isArray, and an undeclared name is safe to test with typeof.

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 null is 'object'. This is a bug from the first version of the language that cannot be fixed without breaking the web. Test for null with value === null.
  • typeof [] is 'object', and so are dates, regular expressions, and maps. Use Array.isArray(value) for arrays, and value instanceof Date or Object.prototype.toString.call(value) for others.
  • typeof function () {} and typeof class {} are 'function'. Functions are objects with extra behavior, and 'function' is a special case of the operator.
  • typeof NaN is 'number', because NaN is a special numeric value.
  • typeof undeclaredName is 'undefined' and does not throw, while reading an undeclared name throws a ReferenceError. That is why typeof 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 with new differ from primitive strings. Avoid them.
  • typeof 10n is 'bigint', and typeof 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.

javascript
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.