JavaScript Operators: The Basics You Need to Know

In this blog we are gonna study about operators in js. you have also studied a bit about operators in schools . Let deep dive into what is operator and different types of operators in js.
Operators-> operator are used to perform operations on operand.
let just take a simple example.
5+7 = 12
so here 5 and 7 are operands and + is operator.
The types of operator we are gonna cover in this blog:
Arithmetic operators (+, -, *, /, %)
Comparison operators (==, ===, !=, >, <)
Logical operators (&&, ||, !)
Assignment operators (=, +=, -=)
Arithmetic operator:
This is the most simplest operator , we have also studied about arithmetic operator in school.
Arithmetic operators are + , - , * , / , % .
Let's understand this basic operator with the help of example.
let num1=5;
let num2=7;
let res;
res= a+b;
console.log("the result of a+b is ",res)
res=a/b;
console.log("the result of a/b is ",res)
res=a%b;
console.log("the result of a%b is ",res)
Output:
the result of a+b is 12
the result of a/b is 0.71
the result of a%b is 5
the / operator returns quotient and %(modulo) returns us the remainder.
Comparison operators (==, ===, !=, >, <):
Comparison operator are also known as relational operator. Comparison operator are used to compare two values. We have also studied about this operators in basic maths that less than or grater than symbol. let understand this operator in js .
In js relational operator return us a boolean value as result for every comparison. Let take a simple example to check whether which number is greater from two numbers.
let num1=5;
let num2=7;
console.log(num1>num2)
Output:
false
Here false state that num1 is not greater than num2.
Lets look at more comparison operator ===, ==, !=
=== ,== both are use to check whether the values are equal or not but there is differnce the == is checks with loose checking and other one follows strict checking. let understand what is loose and strict equality.
The loose equality supports type conversion .
console.log(1=="1")
//Output
true
the == just checks for value not for data type , in the above example the string value is converted to a number.
The strict equality does not allow type conversion and checks for both value and data type
console.log(1==="1")
//Output
false
here string of 1 not converted to number and types of both values are also compared .
!= operator:
the not equal to operator checks for that the two values are not equal . It returns true if two values are not equal and false if two values are equal.
console.log(3!=3)
//Output
false
console.log(3!=4)
//Output
true
Logical operators:
There are three logical operator logical AND(&&) , OR ( | | ) , NOT ( ! ).
We use logical operators to combine multiple conditions.
Logical OR | | :
It is a binary operator and return true if at-least one of its argument is true and if all arguments are false it returns false.
we can understand it with a truth table:
let take some example to understand more about logical or .
console.log(true || false)
//Output
true
console.log(false || false)
//Output
false
let hour = 9;
if (hour < 10 || hour > 18) {
console.log( 'The office is closed.' );
}
//Output
The office is closed.
here the code written inside the if body when expression return inside condition block contains true and here logical operator return true because the condition hour<10 is true and logical or return true when there is at least one truthy value.
In js the logical operators are more powerful as compared to other languages. The logical or also works with non boolean values like strings , numbers . The logical or returns the first truthy value for non boolean values. Let understand this with a example.
let firstName = "";
let lastName = "";
let nickName = "SuperCoder";
console.log( firstName || lastName || nickName || "Anonymous");
//Output
Supercoder
the empty strings are considered as false values and non empty strings are considered as truth value so in above example the first truthy value is Supercoder which is returned.
Logical AND && :
It is a binary operator and return true if both its argument is true and if at-least one argument is false it returns false.
we can understand it with a truth table:
let take some example to understand more about logical or .
console.log(true && true)
//Output
true
console.log(true && false)
//Output
false
let hour = 12;
let minute = 30;
if (hour == 12 && minute == 30) {
console.log( 'The time is 12:30' );
}
//Output
The time is 12:30.
The logical and return true when all the arguments are true.
The logical and returns the first false value for non boolean values. For each operand, converts it to a boolean. If the result is false, stops and returns the original value of that operand. If all operands have been evaluated (i.e. all were truthy), returns the last operand Let understand this with a example.
console.log("Hello" && 100);
//Output
100
here both value are true but non-boolean so it return the last truthy value .
console.log(0 && "Hi");
//Output
0
here 0 is treated as false value so if any value is false in arguments of logical and the first false value is returned.
Assignment operators (=, +=, -=):
This are the most easiest operator let understand them
= operator is assignment operator just assign right value to left side.
let num=5;
Now the num variable now contains the value 5.
let num=5;
num+=2;
console.log(num)
//Output
num
+= means add and assign first add the some value to previous value and the assign it to the same variable and -= , *= , /= , %= works in the same way.
Conclusion:
In this blog, we learned about operators in JavaScript and how they help us perform different operations on values (operands). We explored important types of operators such as arithmetic, comparison, logical, and assignment operators.
Arithmetic operators help us perform mathematical calculations, while comparison operators allow us to compare values and return boolean results. Logical operators are useful for combining multiple conditions and making decisions in programs, and assignment operators help us store and update values efficiently.
Understanding operators is very important because they are used in almost every JavaScript program — from simple calculations to complex decision-making. By mastering these operators, you build a strong foundation for writing cleaner and more effective JavaScript code.

