ak2229
ak2229

Reputation: 33

Js error with && operator

var hp ='geo'
2==2 && hp += 'jio'

why i am getting error as :- Reference Error: Invalid left hand side in assignment

''//''///'//

Upvotes: 2

Views: 1498

Answers (1)

Yosvel Quintero
Yosvel Quintero

Reputation: 19070

There was an unexpected assignment at hp += 'jio' preceded by the logical operator &&. That is the reason to get error Invalid left-hand side in assignment.

When working with expressions and operators notice that the precedence of operators, in this case &&, determines the order they are applied when evaluating an expression. You can override operator precedence by using parentheses.

In this case, if you want to modify the variable hp, you should do it using parentheses because with the parentheses the right-hand side of the initialization is executed as a single expression.

Code:

var hp = 'geo';

2 == 2 && (hp += 'jio');

console.log(hp);

Upvotes: 1

Related Questions