Is there any order in operators in JavaScript?

Photo by Christopher Gower on Unsplash

Introduction

Javascript is a programming or scripting language that allows the implementation of features on the web page. You need to have a basic understanding of HTML and CSS to learn JavaScript.In this article, I’m going to talk about the operators in JavaScript.

JavaScript Operators

JavaScript operators are used to performing particular actions. They can be used to assign values, compare values, combine values, perform arithmetic operators and more. The operators are symbols or keywords.

Arithmetic operators

Arithmetic operators take numerical values as their operands and return a single numerical value.

  1. 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.

Operator Precedence

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 is the main point I want to talk about in this article. That is the operator precedence in JavaScript.Although we use the addition sign(+) first, it catches the multiplication first. Now I’ll give you the operator precedence.

From https://developer.mozilla.org/en-US/

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.

Assignment Operators

An assignment operator assigns a value to its left operand(a variable in JavaScript code) based on the value of its right operand.

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);
Output
x is 7

It can be also used to concatenate or add two strings together.

Examplevar x = "Hi everyone. ";var y = "Welcome to Javascript";x+=y; //same as x= x+yconsole.log(x);OutputHi everyone. Welcome to Javascript

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

There are more operators like advanced assignment operators, comparison operators, logical and conditional operators in JavaScript. I will talk about them in a future article.

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.

Undergraduate at Faculty of Information Technology, University of Moratuwa