admin管理员组文章数量:1198624
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.
This if statement generates an error:
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.
Any ideas? There has to be a way to do this...
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.
This if statement generates an error:
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.
Any ideas? There has to be a way to do this...
Share Improve this question edited Nov 17, 2011 at 21:52 amit_g 31.3k8 gold badges66 silver badges122 bronze badges asked Nov 17, 2011 at 21:50 hokeyplyr48hokeyplyr48 951 gold badge2 silver badges6 bronze badges 08 Answers
Reset to default 12'undefined'
needs to have quotes around it when used with typeof
if(typeof ticketType != 'undefined' && ticketType == 1){}
undefined should be within quotes...
if (typeof ticketType !== "undefined" && ticketType == 1)
{
}
EDIT
Here we are not talking about global.undefined which doesn't have to be enclosed within quotes. We are talking about the return type of typeof operator which is a string. Incidentally for undefined variable, the typeof returns "undefined" and thus we need to enclose it within string.
// 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
.
Wrap it in parenthesis.
if (typeof(ticketType) !== 'undefined' && ticketType === 1)
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 ticketType
is an unknown identifier. However if we first declare it (even without assigning it any value) it becomes known but with undefined
value.
if(typeof ticketType != 'undefined' && ticketType == 1){}
I think you need the quotes around the undefined word. At least I always do it that way.
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:
typeof somevar === 'undefined'
The correct syntax is:
if (typeof ticketType !== 'undefined' && ticketType === 1) { }
The result of the typeof
operator is always a string. See here: http://www.javascriptkit.com/javatutors/determinevar2.shtml
simlpe using console.log(someVar);
本文标签: JavaScriptCheck if variable exists and if equal to valueStack Overflow
版权声明:本文标题:JavaScript : Check if variable exists and if equal to value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738579697a2101088.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论