Operator precedence in JS
Arithmetic operators take numerical values as their operands and return a single numerical value.
- Also, the operator (+)returns the sum of its numerical operands
Examplevar a = 10;var b = 20;var sum = a+b;console.log("Summation is "+sum);OutputSummation is 30
According to the addition arithmetic operator, we can understand the behaviour of subtraction(-), multiplication(*), division(/) ,modules(%) and the exponentiation (**) operator.
Examplevar a= 10;var b= 20;var c= 25;var d = 2;var multiplication = a*b;console.log("The answer is "+multiplication);var modulas = c%a;console.log("The answer is "+modulas);var power = a**d;console.log("The answer is "+power);OutputThe answer is 200
The answer is 5
The answer is 100
2. The increment operator adds 1 to the operand and the decrement operator deducts 1 from it.
There are two ways to use increment and decrement operators.
Examplevar a= 10;var b= a++;console.log("Result after postincrement "+b);var c= 15;var d = ++c;console.log("Result after preincrement "+d);OutputResult after postincrement 10
Result after preincrement 16
In the first one, the increment is done after assigning 10 to the b. In the second one first, c is incrementing by 1 and after it is assigned to the d. Also, the decrement operator behaves.
If we use two or more operators for one variable, sometimes the result would not be the same as we think.
var a = 100+50*3;console.log(a);
To the above example, you can think of 450 as the answer. Because 100+50=150 and 150*3= 450. But the output is 250. What do you think about that?
This table shows the precedence of the operators. As the multiplication operator is the higher place(in 15th) contrasting to the addition operator the multiplication operator acts first.
In this table, the brackets are the highest position, so if you need to do any operation at first, you need to use brackets. And in some operators, we should consider the associativity, and we have to order our codes go get the output.
1.We use only one equal sign to assign a value to the variable.
Examplevar x,y;x=4; //assigns 4 to xy=x;//assigns 4 to yconsole.log("Now y is "+y);OutputNow y is 4
2.The addition assignment operator adds the value of the left and right operands then assigns the sum to the left.
Examplevar x = 4;var y = 3;x+=y; //same as x= x+yconsole.log("x is "+x);
Outputx is 7
It can be also used to concatenate or add two strings together.
Like the addition assignment operator the subtraction, multiplication, division, remainder and exponentiation assignment operators are also having the same process.
Examplevar a = 10;var c = 30;var d =15;a *=c; //same as a=a*c :a=10*10console.log("Now a is "+a);d%=a; //d =10%10=5console.log("Now d is "+d);a **=d; //a=a^d: a=10^5console.log("Now a is "+a);OutputNow a is 100
Now d is 5
Now a is 100000
I think you got some idea about how the operators behave in JavaScriprt, and how to use them to get the required output.
Thank you for reading. If you have any questions feel free to the response.