Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript快速变量不存在_Javascript_Variables - Fatal编程技术网

Javascript快速变量不存在

Javascript快速变量不存在,javascript,variables,Javascript,Variables,我正在寻找一种不检查布尔变量是否存在的快速方法,例如: if(variable){ alert(variable); } 但是在这里我可能会得到“ReferenceError:variableisndefined”使用typeof检查变量是否存在 if((typeof variable !== "undefined") && variable) { ... } 使用typeof检查变量是否存在 if((typeof variable !== "undefined")

我正在寻找一种不检查布尔变量是否存在的快速方法,例如:

if(variable){
   alert(variable);
}

但是在这里我可能会得到“ReferenceError:variableisndefined”

使用
typeof
检查变量是否存在

if((typeof variable !== "undefined") && variable) {
  ...
}

使用
typeof
检查变量是否存在

if((typeof variable !== "undefined") && variable) {
  ...
}
试一试

试一试


如何在工作代码中不存在变量?(除非您包含多个不同的源代码)实际上,javascript中没有“不存在的变量”这样的东西。变量总是存在的。您可能在询问一个“全局变量”,它是
窗口
对象的属性,在这种情况下,您要查找
if(窗口中的名称)…。
工作代码中怎么可能不存在变量?(除非您包含多个不同的源代码)实际上,javascript中没有“不存在的变量”这样的东西。变量总是存在的。您可能在询问一个“全局变量”,它是
窗口
对象的一个属性,在这种情况下,您要查找
if(窗口中的名称).