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?

Share Improve this question edited Nov 15, 2015 at 16:48 Saturnix asked Aug 29, 2013 at 9:59 SaturnixSaturnix 10.6k18 gold badges68 silver badges124 bronze badges 7
  • 2 Yes you can. It would be a better approach here to use if (myVar && myVar == "test") here. – user1983983 Commented Aug 29, 2013 at 10:02
  • Yes && operator is short circuit operator, so other condition will not be checked if first condition is false – Voonic Commented Aug 29, 2013 at 10:02
  • yes, but the code crashes as you try to access an undefined variable – Saturnix Commented Aug 29, 2013 at 10:03
  • Use === instead of == and !== instead of !=. – kamituel Commented Aug 29, 2013 at 10:04
  • Exact duplicate of Javascript conditional order evaluation – Bergi Commented Aug 29, 2013 at 10:10
 |  Show 2 more comments

6 Answers 6

Reset to default 13

Can I assume myVar == "test" will never be executed if myVar 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