This question already has an answer here:
这个问题在这里已有答案:
- JavaScript check if variable exists (is defined/initialized) 29 answers
- JavaScript检查变量是否存在(已定义/初始化)29个答案
How can i check in JavaScript if a variable is defined in a page? Suppose I want to check if a variable named "x" is defined in a page, if I do if(x != null)
, it gives me an error.
如果在页面中定义变量,我如何签入JavaScript?假设我想检查一个名为“x”的变量是否在页面中定义,如果我这样做(x!= null),它会给我一个错误。
7 个解决方案
#1
151
I got it to work using if (typeof(x) != "undefined")
我用if(typeof(x)!=“undefined”)让它工作
#2
47
To avoid accidental assignment, I make a habit of reversing the order of the conditional expression:
为了避免意外分配,我养成了颠倒条件表达式顺序的习惯:
if ('undefined' !== typeof x) {
#3
21
The typeof operator, unlike the other operators, doens't throws a ReferenceError exception when used with an undeclared symbol, so its safe to use...
与其他运算符不同,typeof运算符在与未声明的符号一起使用时不会抛出ReferenceError异常,因此可以安全使用...
if (typeof a != "undefined") {
a();
}
#4
1
You can do that with:
你可以这样做:
if (window.x !== undefined) { // You code here }
if(window.x!== undefined){//你在这里编码}
#5
1
As others have mentioned, the typeof
operator can evaluate even an undeclared identifier without throwing an error.
正如其他人所提到的,typeof运算符甚至可以在不抛出错误的情况下评估未声明的标识符。
alert (typeof sdgfsdgsd);
Will show "undefined," where something like
将显示“未定义”,类似的东西
alert (sdgfsdgsd);
will throw a ReferenceError.
将抛出一个ReferenceError。
#6
0
Assuming your function or variable is defined in the typical "global" (see: window's) scope, I much prefer:
假设您的函数或变量是在典型的“全局”(参见:窗口)范围内定义的,我更喜欢:
if (window.a != null) {
a();
}
or even the following, if you're checking for a function's existence:
如果您正在检查函数是否存在,甚至是以下内容:
if (window.a) a();
#7
-3
try to use undefined
尝试使用undefined
if (x !== undefined)
This is how checks for specific Browser features are done.
这是检查特定浏览器功能的方法。
#1
151
I got it to work using if (typeof(x) != "undefined")
我用if(typeof(x)!=“undefined”)让它工作
#2
47
To avoid accidental assignment, I make a habit of reversing the order of the conditional expression:
为了避免意外分配,我养成了颠倒条件表达式顺序的习惯:
if ('undefined' !== typeof x) {
#3
21
The typeof operator, unlike the other operators, doens't throws a ReferenceError exception when used with an undeclared symbol, so its safe to use...
与其他运算符不同,typeof运算符在与未声明的符号一起使用时不会抛出ReferenceError异常,因此可以安全使用...
if (typeof a != "undefined") {
a();
}
#4
1
You can do that with:
你可以这样做:
if (window.x !== undefined) { // You code here }
if(window.x!== undefined){//你在这里编码}
#5
1
As others have mentioned, the typeof
operator can evaluate even an undeclared identifier without throwing an error.
正如其他人所提到的,typeof运算符甚至可以在不抛出错误的情况下评估未声明的标识符。
alert (typeof sdgfsdgsd);
Will show "undefined," where something like
将显示“未定义”,类似的东西
alert (sdgfsdgsd);
will throw a ReferenceError.
将抛出一个ReferenceError。
#6
0
Assuming your function or variable is defined in the typical "global" (see: window's) scope, I much prefer:
假设您的函数或变量是在典型的“全局”(参见:窗口)范围内定义的,我更喜欢:
if (window.a != null) {
a();
}
or even the following, if you're checking for a function's existence:
如果您正在检查函数是否存在,甚至是以下内容:
if (window.a) a();
#7
-3
try to use undefined
尝试使用undefined
if (x !== undefined)
This is how checks for specific Browser features are done.
这是检查特定浏览器功能的方法。