The use of undefined variables in if-statements

前端 未结 6 1742
别跟我提以往
别跟我提以往 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:35

    This is not a strange question coz I'm on the same boat :p

    I reach here after googling "javascript use variable undefined". I got confuse coz I've seen somewhere that people can simply use undefined variable in a loop.

    Maybe I saw it on different language


    I've found it. The first codes successfully executed while the second codes runtime error.

    First Codes:

    test();
    function test() { x = 4; }
    console.log(x); //this produce 4
    

    Second Codes:

    test();
    function test() { x[0] = 1; }
    console.log(x[0]);
    

提交回复
热议问题