admin管理员组文章数量:1173626
In order to protect my code from accessing undeclared variables I use
if (typeof myVar != 'undefined')
This works fine but I'd like to stick another if statement in it. Something like converting this:
if (typeof myVar != 'undefined'){
if (myVar == "test"){}
}
to this:
if (typeof myVar != 'undefined' && myVar == "test")
Considering myVar
may be undefined, is this last code secure in every case of usage and every browser?
Is it possible that various statements inside an if ()
are not evaluated in the order they're written?
Can I assume myVar == "test"
will never be executed if myVar
is undefined?
In order to protect my code from accessing undeclared variables I use
if (typeof myVar != 'undefined')
This works fine but I'd like to stick another if statement in it. Something like converting this:
if (typeof myVar != 'undefined'){
if (myVar == "test"){}
}
to this:
if (typeof myVar != 'undefined' && myVar == "test")
Considering myVar
may be undefined, is this last code secure in every case of usage and every browser?
Is it possible that various statements inside an if ()
are not evaluated in the order they're written?
Can I assume myVar == "test"
will never be executed if myVar
is undefined?
6 Answers
Reset to default 13Can I assume
myVar == "test"
will never be executed ifmyVar
is undefined?
Yes. The expression you are testing is a logical AND expression and the order of evaluation of the two operands is specified:
The production LogicalANDExpression
:
LogicalANDExpression&&
BitwiseORExpression is evaluated as follows (emphasis added):
- Let lref be the result of evaluating LogicalANDExpression.
- Let lval be GetValue(lref).
- If ToBoolean(lval) is false, return lval.
- Let rref be the result of evaluating BitwiseORExpression.
- Return GetValue(rref).
That basically says evaluate the first operand, if the result of that evaluation is false
, the entire expression is false
, and the second operand is never evaluated.
Yes if you are using && operator then if first condition is false then second will never test.
Yes, it is OK.
In javascript, the precedence is as below:
typeof
> !=/==
> &&
For a && b
, b
will never be executed when a
is false.
if (typeof myVar != 'undefined' && myVar == "test")
The expression is evaluated left to right so it will short circuit if myVar
is undefined
(myVar will not be compared to "test")
Whenever you use the && operator, the conditions that you pass in will be evaluated in the order that they were passed in.
In your case:
if (typef myVar != 'undefined' && myVar == 'test')
When the program enters this if statement, it first checks if the variable type is undefined, if it returns true, it continues to the second argument and checks if myVar has a value of 'test'. Even if myVar == 'test', the if statement will evaluate the value of false && true, which returns true. Only if both conditions return true, the code block will get executed, because the expression will be evaluated as true (true && true).
If the first condition
typeof myVar != 'undefined'
returns false, the second condition will never get evaluated.
Very Important! Use
if (myVar !== undefined)
This means that is no way that myVar will pass if it had any assignment before.
AND DONT USE 'undefined'
THIS IS A SIMPLE STRING!
Good luck!
本文标签: Order of evaluation in if statement in JavascriptStack Overflow
版权声明:本文标题:Order of evaluation in if statement in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737987521a2044996.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
if (myVar && myVar == "test")
here. – user1983983 Commented Aug 29, 2013 at 10:02===
instead of==
and!==
instead of!=
. – kamituel Commented Aug 29, 2013 at 10:04