rwaldron / idiomatic.js

Principles of Writing Consistent, Idiomatic JavaScript
Other
24.66k stars 3.46k forks source link

typeof variable === "object" potentially problematic #249

Open mjsarfatti opened 4 years ago

mjsarfatti commented 4 years ago

On the type checking section the following is recommended to see if a variable is an object:

typeof variable === "object"

The problem is that:

typeof null === "object"

So the recommendation should probably read:

typeof variable === "object" && variable !== null
ferdnyc commented 8 months ago

Though, some coders will prefer to shorten that to,

typeof variable === "object" && variable

(That works because, if variable is an object, only null will test false.)