Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

Updated
•5 min read•View as Markdown
JavaScript Operators: The Basics You Need to Know

In programming one of the concept come that is operators. It is used for the mathematical operations calculations which make our code logical and also make decision on basis of program in our codebase.

What is Operator ?

Operators are the symbols that tells us to perform specific operation on one or more value.

Example :

let a = 10;
let b = 20;
let sum = a + b;
console.log(sum);

Here the a and b are variable having their own values and sum is also a variable but it is instructed to do some arithmetic calculations of a and b. Here a and b are operands and the the sum instructed to do is the operators which is denoted by symbols. It use to do arithmetic calculations.

Types of Operators

Here we have few major operators like

  • Arithmetic operators

  • Comparison Operators

  • Logical Operators

  • Assignment Operators

Arithmetic Operator

We have majorly five arithmetic operators to perform specific task all of them have different roles to do.

  • Addition : It is denoted by the symbol ( + ) which use to do addition of two or more values ( 5 + 3 ).

  • Subtractions : It is denoted by the symbol ( - ) which usually do the subtractions between two or more values ( 5 - 3 ).

  • Multiplication : It is denoted by the symbol ( ) which usually do the multiplication of values ( 5 3 ).

  • Division : It is denoted by the symbol ( / ) which is used to do the division of values ( 5 / 3 ).

  • Modulus : It is denoted by the symbol ( % ) which gives us the remainder after dividing the two values ( 5 % 3 ). Here we will get 2 because after dividing 5 with three we get the remainder 2.

Comparison Operators

We have a Comparison Operators which use to do the comparison between two or more values and give a desired output on basis of the logic we have build which returns the value in true or false ( Boolean ). We have few comparison operators are equals to, equals to ( strict ), greater than, less than, not equals to.

Equals to ( == ) : It is used to compare the value that the both values are same or not.

Equals to ( === ) : Here we check strictly that both value and the data types too ( 8 === 8 ).

Greater than ( > ) : This operator is used to check that left value is greater or not ( 8 > 5 ).

Less than ( < ) : This is the operator which check that the right value is greater or not ( 8 < 5 ).

Not equals to ( !== ) : It is used to reverse the true value to false one and vies versa ( 8 !== 5 ).

Logical Operator

We also have logical operators which make a decision on certain conditions that has been provided to test. It test multiple conditions. We have three logical operators And ( && ), Or ( || ) and the not ( ! ).

AND logical operator compares the all condition need to be true which have been provided him to test and after that only it will give the true.

Example :

let age = 20; 
if (age > 18 && age < 30) {
 console.log("You are in the young age group."); 
}

Here the code will execute only if the both condition have to be true.

OR logical operator don't work like that it checks all the condition and if it gets any one of the condition true it will execute the code.

Example :

let day = "Saturday"; 
if (day === "Saturday" || day === "Sunday") {
 console.log("It is the weekend."); 
}

Here if any one of the condition will found true then the code will execute.

NOT logical operator works different it just reverse the true value with the false one and the false one into true.

let isLoggedIn = false;
console.log(!isLoggedIn);

Because NOT converts false to true so the output will be true.

Assignment Operators

Assignment Operator is used to assign a value to a variable. We have commonly three assignment operator =, += and -=. All of the works differently.

Equals ( = ) is used to assign the value to the variable.

Add and assign ( += ) is used to add a value and then assign it.

Subtract and assign ( -= ) is used to subtract a value and then assign it.

Assignment are easier to write and it also make the code shorter.