variable === undefined vs. typeof variable === “undefined”

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.

Leave a Comment