#Confused about parameters being on LHS vs RHS in JavaScript functions

3 messages · Page 1 of 1 (latest)

jagged phoenix
#

Hello! I have a doubt about function parameters and assignment in JavaScript.

function changeBackgroundColor(newColor) {
document.body.style.backgroundColor = newColor;
}
vs
function sendMe(y) {
y = 5;
}

In the first function, the parameter newColor is written on the right-hand side (RHS) of the assignment, even though it’s a parameter.
But in the second function, the parameter y is written on the left-hand side (LHS).
Why is that?

sturdy folio
#

The first one takes the input value and assignes the value of it to the DOM bgColor.

The second version takes the input parameter and assigns the value 5 to it.

Very basically:
The first version is a usual „take the input var and cofigure something with it“ style while the second one is more of a „here is a input var, do something with it“ style.

#

In your case the left part of the equal sign is the target while the right side is the source