Search code examples
javascriptorder-of-executionrelational-operatorsecmascript-2020

What exactly the "LeftFirst" Boolean Flag is in "Abstract Relational Comparison Algorithm" in ECMAScript?


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


Solution

  • 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.