I am looking for a good examples of left/right hand side of an assignment in JS

I am looking for a good examples of left/right hand side of an assignment in JS
0

#1

I just read You Don’t Know Javascript: Scope and Closures chapter one. The LHS/RHS references did not totally sink in. I know I could do a search for this. There are so many coding blogs and articles out there. I decided to post here asking for recommended resources.

Thanks


#2

You assign a value to an identifier (variable). Identifier on the left, assignment operator (=), then the value you want to assign a unique name to on the right:

foo = 1
bar = 2
baz = 3

foo, bar, baz are identifiers, they are on the left of the assignment operator. They are being assigned 1, 2, and 3 respectively, and those values are on the right of the assignment operator.

identifier assignmentOperator value
      |             |           |
    myVar           =         'foo'

There aren’t really any resources per se because that’s it, there isn’t much more to it than that.

var foo = 'hello';
const bar = true;
let baz = [1,2,3];

Note the asssignment operator does not mean equality like in maths, it just uses the same symbol (in some other languages it does, but not Javascript).


#3

Maybe I just need to reread the explanation from YDKJS: Scope & Closures “Compiler Speak” this weekend

…the type of look-up Engine performs affects the outcome of the look-up…

Actually, let’s be a little more precise. An RHS look-up is indistinguishable, for our purposes, from simply a look-up of the value of some variable, whereas the LHS look-up is trying to find the variable container itself, so that it can assign. In this way, RHS doesn’t really mean “right-hand side of an assignment” per se, it just, more accurately, means “not left-hand side”.

Being slightly glib for a moment, you could also think “RHS” instead means “retrieve his/her source (value)”, implying that RHS means “go get the value of…”.

Let’s dig into that deeper.

When I say:

console.log( a );
The reference to a is an RHS reference, because nothing is being assigned to a here. Instead, we’re looking-up to retrieve the value of a, so that the value can be passed to console.log(…).

By contrast:

a = 2;
The reference to a here is an LHS reference, because we don’t actually care what the current value is, we simply want to find the variable as a target for the = 2 assignment operation…


#4

Bear in mind he’s using his own terms here; these aren’t official concepts, he’s just using LHS and RHS to refer to an idea he’s trying to explain.

JavaScript lets you assign and reassign values to identifiers. So you can do let foo = 1 then sometime later foo = 2.

When you do that, the engine will check if foo exists and assign/reassign if necessary. That’s what he’s calling a LHS reference; the only thing you care about is if that identifier exists, not the value.

If you use the variable, like foo + 2, in this case he’s calling it a RHS reference, you only care about the value, you just want to check that foo has a value and what that value is.

I think that makes sense; the concept isn’t that much more complicated than what I originally wrote and I think he’s overcomplicating, or at least not being totally clear with the explanation.


#5

These are real. That is why you get messages like:

ReferenceError: invalid assignment left-hand side


#6

No, I realise that, it’s how he’s using the terms here, I don’t think he’s being particularly clear. Yes you can have an invalid assignment left hand side, but the inverse isn’t true because it doesn’t make sense (he doesn’t suggest it is, but he uses the terms kinda as mirror images in his explanation). LHS and RHS are tems he’s using to make sense of assignment. He does this a few times throughout the books, and to be fair to him he’s always upfront about it just being a way for him to explain the concepts


#7

These seem to be simply made-up terms that likely only make sense in the context of the author’s explanations - the javascript standard mentions the left-hand side in various grammar production rules - I don’t think the author means the same thing

I guess the better question is what real javascript concept or construct are you trying to understand - is it closures and scope? what about them specifically?

I should add there is an extremely important concept of rvalue and lvalue in C++ and C - but their importance and usage presumes a memory model that does not apply to javascript


#8

couple discussions regarding the author’s terminology are found here

as far as I can tell the stackoverflow post and comments by torazaburo are spot-on - it’s very strange his answer has so many downvotes


#9

I recommend you watching the Kyle Simpson’s “Advanced JavaScript” course on Pluralsight (you can google how to get 3 months free there) - https://app.pluralsight.com/library/courses/advanced-javascript/table-of-contents
Scope section


#10

Thank you @Waxaz I am currently watching “JS understanding the weird parts” on Udemy and reading the YDKJS series. I added this course to my list of courses Id like to explore.


#11

Hi there!

I know it is a little bit late but I am reading YDKJS Scope & Closures now. I actually ended up here while searching LHS and RHS reference to understand them properly.

So how is it going? Have you finished YDKJS or Weird parts?