Assignment Operator

There is only one Assignment Operator which is = sign. It assigns the value of its right side operand to the operand on its left side.

let a = 30;
let b;
b = a;
console.log(b);       // 30

The = operator takes the value of a and then assign the value to the variable b. That what all it does. You can use function that returns the value or expression that evaluates to a value and then the assignment operation is done.

let b = getValue();          // Function Call
let x = c + d;               // Expression

Here the returned value from the function getValue() gets assigned to the variable b. And the result of c + d is evaluated first and then the result gets assigned to the variable x.

Left Operand must be Variable

The only rule is that the first variable cannot be literals. It must be variable. For example the following is wrong -

30 = 10;
"Hey" = "Hi";

However, the followings are correct -

let a, hey;
a = 30;
hey = "hi";

Multiple Assignment in One Statement

You can use multiple assignment in one statement. In this case, the same value is assigned to all the variable. Here is the syntax -

variable1 = variable2 = variable3 = value;

The value is assigned to all three variable variable1, variable2 and variable3. So their values become same.

Assignment Operator has the lowest priority

The assignment operator has the lowest priority. It means expression on the right side is evaluated first and then the assignment operation happens.

let a = 20;
let b = 30;
let c = a + b; // c is 50, not 20

Compund Assignment Operator

Consider the following example -

let a = 30;
let b = 45;
a = a + b;

Consider the last statement. The statement like this, we do very often in our real life programming. To make the statement concise, most programming language offers Compund Assignment Operator. They looks like this -

a += b;
a *= b;
a -= b;
a /= b;
a %= b;

The above statement are equivalent to the followings -

a = a + b;
a = a * b;
a = a - b;
a = a / b;
a = a % b;

So the Arithment Operators and Assignment Operators together is used to built Compund Assignment Operators. They are kind of shortcut syntax. You can live without these operators but these are good to know. In the above examples we have used only arithmetic operators along with Assignment Operator. But few programming language offers other variation too. Like in PHP -

$fullName = "Santanu ";
$lastName = "Bera";
$fullName .= $lastName;

// Which is equivalent to the following -
$fullName = $fullName . $lastName; // Dot operator is used for string concatenation.

So there are different compound operators in different programming language. But their application is same in all programming language.