1

Can someone explain what exactly the LeftFirst Boolean Flag is in Abstract Relational Comparison Algorithm in ECMAScript? I know that there is only one operator < handling all other relational operators like >, >=, <= as mentioned in the ECMAScript specification in Abstract Relational Comparison using the LeftFirst Boolean Flag for, and example: when we write and run an operation like 10 > 5 the LeftFirst Boolean Flag becomes false, and the left operand 10 is moved to the right side where the operand 5 is, and the right operand 5 is moved to the left side where the operand 10 earlier was, and the > operator becomes < operator and at last something like this is executed 5 < 10, but now my question is: when executing 5 < 10 I must know whether what operand gets evaluated first, is it the operand 5 or is it the operand 10? I'm asking this because they have not mentioned about this in the ECMAScript specification in the Abstract Relational Comparison Algorithm.

and I must know why the >= is executed with LeftFirst true and why the <= is executed with LeftFirst false. Pls help me

padaleiana
  • 955
  • 1
  • 14
  • 23

1 Answers1

1

The input values x and y in the algorithm description are expected to be fully evaluated before those steps begin. The flag is so that the operations like ToPrimitive() happen in the proper order.

For example, x and y might be object references. The ToPrimitive() operation will call either .toString() or .valueOf() in order to perform the comparison operation. Either of those functions might have side-effects, so the flag makes sure that the operations in the right order according to what the source code actually looks like.

A "side-effect" is a change to program state that happens in a function call. A toString() function can change anything that any other function can change: properties of the object, global variables, anything. The rule makes sure that those changes happen in the correct order (left-side changes before right-side changes), no matter what the algorithm does to "flip" the operands.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • what do you mean by side effects – user13104441 Mar 22 '20 at 13:44
  • @user13104441 I'll extend the answer – Pointy Mar 22 '20 at 13:44
  • so you mean that this rule doesnt apply for primitives in ecmaScript for and example `10 > 5` its executed like `5 < 10` and now you mean the number `5` is evaluated first before number `10` – user13104441 Mar 22 '20 at 13:56
  • @user13104441 yes it doesn't make any difference for primitive values because they're already primitive values; the `ToPrimitive` operation doesn't do anything in that case. – Pointy Mar 22 '20 at 14:12
  • what about objects i mean what will happen if we compare two objects like `obj1 > obj2` this is also executed `LeftFirst` being `false` like `obj2 < obj1` so in this case which object will get evaluated first is it the `obj1` or is it the `obj2` – user13104441 Mar 22 '20 at 14:29