Do not think of it as an assignment on the left or on the right, think of it as storing a value in memory and retrieving it later.
For example, when you type b in the Chrome Developer Console, it starts an RHS search (gets the value b ), and if the value is not found, it raises a ReferenceError .
In contrast, when you type b = 2 in the Chrome Developer Console, it starts an LHS search, and if b is not found in the nested scope, the JS compiler declares it in the global scope (depending on whether you are executing the code in strict mode or not )
For example, consider the following code.
function foo(a) { console.log( a + b) } foo( 2 );
When the JS compiler executes the code, it first searches for the function foo and determines if it is declared in the current area (the hosting environment is here), which is called RHS lookup. Now, in the area foo, the argument will be 2, and when we declared the function foo(a) when we wrote foo( 2 ) we implicitly assign the value 2 a or a = 2 , which is called the LHS lookup (assignment 2 a ), now rewind forward. The compiler will again go to the console.log( a + b) line console.log( a + b) will again search for the values of a and b (again search for RHS ) and, if the values are found, it will assign it to console.log argument (if you assume that console.log defined as console.log(arg1) in the hosting environment, then arg1 = value of a+b (which again is an LHS search).
In short:
• when the mechanism receives the value of the console.log(b) variable, it receives the value from memory b . This is an RHS search
• when the mechanism assigns the value of the variable b = 2 , it looks for the value of b in the scope - if it is found, it sets the value 2 in the memory location b , if not, it looks at the scope of the top level, This is an LHS search.
Pradeepsinh sindhav
source share