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

后端 未结 8 1310
情深已故
情深已故 2020-11-22 14:43

The jQuery Core Style Guidelines suggest two different ways to check whether a variable is defined.

  • Global Variables: typeof variable === "undefined&
8条回答
  •  臣服心动
    2020-11-22 15:25

    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.

提交回复
热议问题