How to check a not-defined variable in JavaScript

后端 未结 14 991
陌清茗
陌清茗 2020-11-22 14:23

I wanted to check whether the variable is defined or not. For example, the following throws a not-defined error

alert( x );

How can I cat

14条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 15:09

    You can also use the ternary conditional-operator:

    var a = "hallo world";
    var a = !a ? document.write("i dont know 'a'") : document.write("a = " + a);

    //var a = "hallo world";
    var a = !a ? document.write("i dont know 'a'") : document.write("a = " + a);

提交回复
热议问题