The jQuery Core Style Guidelines suggest two different ways to check whether a variable is defined.
typeof variable === "undefined"
variable === undefined
object.prop === undefined
Why does jQuery use one approach for global variables and another for locals and properties?
For undeclared variables, typeof foo
will return the string literal "undefined"
, whereas the identity check foo === undefined
would trigger the error "foo is not defined".
For local variables (which you know are declared somewhere), no such error would occur, hence the identity check.