//Syntax var <variable> = < variable or expression>; //Example var myNumber = 334.55; var myExp = a + 3;
- Simple assignment
- Chained assignment
- Complex or Self-assignment
- Destructuring assignment
We shall discuss each one of them one-by-one with suitable examples.
Simple Variable Assignment
You have already seen an example of simple assignment in the earlier section. In this method, you simply assign a value of constant, or a variable or an expression to the variable on the left-hand side of the assignment operator.
Consider the following example,
//variable declaration simple assignment var A = 33; //assigned a constant var B = C; //assigned a variable value var F = 3 * 4; //assigned an expression
//variable declaration chained assignment var A = B = C = 220; //The above is same as var A = 220; var B = 220; var C = 220; var D = 220;
The chained assignment starts with rightmost expression first and then move to the leftmost variable in the expression. The expression A = B = C is same as A = (B = C). The expression in the parenthesis is executed first and then the next pair (A = B).
The self-assignment is compound assignment where the left-hand variable is part of right-hand expression. Consider the following example.
//variable declaration self-assignemnt var x += 10; //It is same as var x = x + 10;
The expression involving left-hand side( x + 10) on the right side is of assignment operator is evaluated first, and then assigned back to the variable x. This is self-assignment.
Note that the expression x += 10 can also be written as x = x + 10;
|Assignment Expression||Expression||Standard Form|
|addition||x += 1||x = x + 1|
|subtraction||x -= 1||x = x – 1|
|multiplication||x *= 1||x = x * 1|
|division||x /= 1||x = x / 1|
|remainder||x %= 1||x = x % 1|
|exponential||x **=1||x = x ** 1|
|right shift||x >>= 1||x = x >> 1|
|left shift||x <<= 1||x = x << 1|
|unsigned right shift||x >>>= 1||x = x >>> 1|
|logical AND||x &&=1||x = x && 1|
|logical OR||x ||= 1||x = x || 1|
|bitwise AND||x &= 1||x = x &= 1|
|bitwise OR||x |= 1||x = x | 1|
|bitwise XOR||x ^= 1||x = x ^ 1|
The destructuring allows to quickly assign the values of an array to variables. Consider the following example, where we accessed each element of the by its index and assigned it to another variables.
//array declaration var fruits = ["oranges","apples","banana", "mangoes"]; //assigning values of array to variables var item1 = fruits; var item2 = fruits; var item3 = fruits; var item4 = fruits;
The same can be does in few steps using destructuring. See the same example with destructuring below.
//array declaration var fruits = ["oranges","apples","banana", "mangoes"]; //destructuring array var [item1, item2, item3, item4] = fruits; //output to console console.log(item1); console.log(item2); console.log(item3); console.log(item4);
The output of the above program is given below.
You can clearly see how easily we were able to assign values of array to variables using destructuing assignment.
In the next post, we will discuss about arithmetic operators.