JavaScript:检查变量是否存在以及是否等于值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8174844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript : Check if variable exists and if equal to value
提问by hokeyplyr48
I have three pages utilizing the same code and on one of the pages this variable doesn't exist, on the other two the variable ticketType has a value of 1 or 2. I need to first check if ticketType exists and isn't undefined and secondly, need to determine if it's one or 2.
我有三个使用相同代码的页面,其中一个页面上这个变量不存在,另外两个页面上的变量 ticketType 的值为 1 或 2。我需要首先检查 ticketType 是否存在并且不是未定义的其次,需要确定它是一还是二。
This if statement generates an error:
这个 if 语句会产生一个错误:
if(typeof ticketType != undefined && ticketType == 1){}
It's saying ticketType isn't defined. I tried nesting the if statements to check if it was defined first thinking it wouldn't go and try the inner if statement but firebug still generates an error.
是说ticketType isn't defined。我尝试嵌套 if 语句以检查它是否已定义,首先认为它不会去尝试内部 if 语句,但 firebug 仍然会产生错误。
Any ideas? There has to be a way to do this...
有任何想法吗?必须有办法做到这一点......
回答by locrizak
'undefined'needs to have quotes around it when used with typeof
'undefined'使用时需要在它周围加上引号 typeof
if(typeof ticketType != 'undefined' && ticketType == 1){}
回答by amit_g
undefined should be within quotes...
undefined 应该在引号内...
if (typeof ticketType !== "undefined" && ticketType == 1)
{
}
EDIT
编辑
Here we are not talking about global.undefinedwhich doesn't have to be enclosed within quotes. We are talking about the return type of typeof operatorwhich is a string. Incidentally for undefined variable, the typeof returns "undefined" and thus we need to enclose it within string.
这里我们不是在谈论global.undefined,它不必用引号括起来。我们正在谈论typeof 运算符的返回类型,它是一个字符串。顺便提一下,对于 undefined 变量,typeof 返回“undefined”,因此我们需要将它包含在字符串中。
// ticketType is not defined yet
(typeof ticketType !== undefined) // This is true
(typeof ticketType === undefined) // This is false
(typeof ticketType !== "undefined") // This is false
(typeof ticketType === "undefined") // This is true
var ticketType = "someValue"; // ticketType is defined
(typeof ticketType !== undefined) // This is still true
(typeof ticketType === undefined) // This is still false
(typeof ticketType !== "undefined") // This is true
(typeof ticketType === "undefined") // This is false
So the correct check is against "undefined"not against global.undefined.
所以正确的检查是针对"undefined"而不是针对global.undefined.
回答by zzzzBov
you're incorrectly checking for an undefined variable, but if you're planning on using it, why not just make sure that it gets defined?
你错误地检查了一个未定义的变量,但如果你打算使用它,为什么不确保它被定义?
ticketType = ticketType || 1;
if (ticketType === 1) {
//do stuff
}
As everyone else has shown, the standard way of checking for undefined values in JS is:
正如其他人所展示的,在 JS 中检查未定义值的标准方法是:
typeof somevar === 'undefined'
回答by Kai Qing
if(typeof ticketType != 'undefined' && ticketType == 1){}
I think you need the quotes around the undefined word. At least I always do it that way.
我认为您需要在未定义的单词周围加上引号。至少我总是那样做。
回答by T. Stone
Wrap it in parenthesis.
把它包在括号里。
if (typeof(ticketType) !== 'undefined' && ticketType === 1)
if (typeof(ticketType) !== 'undefined' && ticketType === 1)
回答by Jonathan M
The correct syntax is:
正确的语法是:
if (typeof ticketType !== 'undefined' && ticketType === 1) { }
The result of the typeofoperator is always a string. See here: http://www.javascriptkit.com/javatutors/determinevar2.shtml
typeof运算符的结果始终是一个字符串。见这里:http: //www.javascriptkit.com/javatutors/determinevar2.shtml
回答by Tomasz Nurkiewicz
The error message is pretty clear. Try with this:
错误信息非常清楚。试试这个:
var ticketType;
if(typeof ticketType != undefined && ticketType == 1){}
You cannot just reference a variable that does not exist. You can only do this when assigning:
你不能只引用一个不存在的变量。您只能在分配时执行此操作:
ticketType = 1;
The browser complaints because ticketTypeis an unknown identifier. However if we first declare it (even without assigning it any value) it becomes known but with undefinedvalue.
浏览器抱怨,因为ticketType是一个未知的标识符。但是,如果我们首先声明它(即使没有为其分配任何值),它就会变得已知但具有undefined值。
回答by Faris Rayhan
simlpe using console.log(someVar);
simlpe 使用 console.log(someVar);

