19

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?

Saturnix
  • 10,130
  • 17
  • 64
  • 120

6 Answers6

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.

Community
  • 1
  • 1
James Allardice
  • 164,175
  • 21
  • 332
  • 312
6

Yes, it is OK.

In javascript, the precedence is as below:

typeof > !=/== > &&

For a && b, b will never be executed when a is false.

xdazz
  • 158,678
  • 38
  • 247
  • 274
5

Yes if you are using && operator then if first condition is false then second will never test.

Butani Vijay
  • 4,181
  • 2
  • 29
  • 61
3
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")

Darren
  • 68,902
  • 24
  • 138
  • 144
2

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.

vladzam
  • 5,462
  • 6
  • 30
  • 36
-1

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!

JohnnyJS
  • 1,320
  • 10
  • 21