What is the difference in Javascript between 'undefined' and 'not defined'?

前端 未结 8 2211
半阙折子戏
半阙折子戏 2020-11-29 03:21

If you attempt to use a variable that does not exist and has not been declared, javascript will throw an error. var name is not defined, and the script will sto

8条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 03:56

    I believe it's a difference between JavaScript definition and firebugs of the same thing.

    Undefined is just the state of something that has not been defined as a value. So it has no type.

    Also what is the difference between var a; and var b = undefined;

    var a; alert(a); // undefined
    a; alert(a); // "Error "a" not defined"
    a = undefined; alert(a); // undefined
    

    2nd example is not valid JavaScript code and the execution will stop. Since this is an error.

提交回复
热议问题