CodePark/ questions / Types & coercion
START PRACTICINGPRACTICE
Frontend question · JavaScript › Types & coercion

What does this log?

console.log(typeof null, typeof undefined, typeof [], typeof function () {});
object undefined object function

typeof null is 'object'. This is a bug from the first JavaScript implementation, and it was kept so old code would not break. Arrays are objects, so typeof [] is also 'object'. To check for an array, use Array.isArray. Functions get their own result, 'function'.

The tempting option "null undefined array function" is wrong because
typeof never returns 'null' or 'array'.