Skip to content

Comment on You Can't JavaScript Under Pressure

Comments

Last one reminded me how awful JS is once again. There are 5 different ways to check if an object is an array, some are implementation dependent, and the obvious one doesn't work (typeof x == "array").

typeof(x) === typeof(["something which is certainly", "an array"])

or am i missing something?

     > typeof ["a", "b"]

    "object"
Of course, since you're told you'll only get numbers, arrays, and strings, that is enough to solve the problem narrowly.

x instanceof Array

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.