0

I've heard about something called operator evaluation. Now I'm asking you to tell me if I'm right:

So take a look at the following code

var a = 5;
false && (a = 2);
console.log(a);

the result in this code will remain 5 that's because the && operator will look first at the first operand and after that it will look at the second operand. and because the first operand is false we wont get to the second operand so it wont get executed.

This thing called an operator evaluation and this operator evaluation applied to any operator. it first look at the first operand(first meaning the one on the left side) and after that the second(right side)

Am I right ?

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
  • 1
    Does this answer your question? [Does JavaScript have "Short-circuit" evaluation?](https://stackoverflow.com/questions/12554578/does-javascript-have-short-circuit-evaluation) – adamgy Apr 08 '21 at 19:51
  • Maybe you are looking for this? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#assignment_operators – Camilo Apr 08 '21 at 19:53
  • 1
    "*this thing called an operator evaluation*" - not really – Bergi Apr 08 '21 at 19:54
  • 1
    @MisterJojo what do you mean? I just tried it out and works as expected. – Camilo Apr 08 '21 at 19:58
  • 1
    I think the term you're looking for is "short-circuit evaluation". – Pointy Apr 08 '21 at 19:58
  • @Camilo I do a lot of TPM. If this kind of code actually passes to the interpreter, this syntax hijacking should be avoided. Its ambiguity turns into a nightmare for those who will have to maintain and develop this kind of code, Programmers end up building complex conditions in part one, then they do the same on part two. – Mister Jojo Apr 08 '21 at 22:11

0 Answers0