The use of undefined variables in if-statements

前端 未结 6 1753
别跟我提以往
别跟我提以往 2021-02-05 10:06

This snippet results in a JavaScript runtime error: (foo is not defined)

if (foo) {
    // ...
}

I have to define foo

6条回答
  •  情深已故
    2021-02-05 10:47

    It seems strange question to me. Lets say I wanted to call a function:-

    thing();

    If thing hasn't be defined anywhere surely I would expect it to fail.

    Why would you expect to use a variable called foo without defining it anywhere in your code?

提交回复
热议问题