Playing with JS Operators
Most programming languages have symbols which helps them perform different operations like comparison, addition, division etc. Those symbols are known as operators.
In this blog we are are going to discuss some basic operators that you would use almost daily in your javascript journey.
Arithmetic Operators
As the name suggests, arithmetic operators help programmers perform arithmetic operations. Below is a list of arithmetic operators:
OPERATOR | MEANING |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (remainder) |
** | Exponentiation |
console.log(5+2) //7
console.log(5-2) //3
console.log(5*2) //10
console.log(5/2) //2.5
console.log(5%2) //1
console.log(5**2) //25
Assignment Operators
Once again, as the name suggests, assignment operators are the ones which you shall be using to assign values to variables. Lets know more about them directly through code;
let x= 10; // = is an assignment operator
x+= 5; //+= is a shorthand to add and assign, it simply means x=x+5;
x*= 2; //x =x*2
x-=2; //x=x-2
x/=2; //x=x/2
x%=2; //x=x%2
x**=2; //x= x**2
Comparison Operators
You know what I might say isn't it? But here we go again.
Comparison Operators as the name suggests are the operators which you would be using too compare two values. It returns either true or false. Below is a list of comparison operators you must be aware of:
OPERATOR | MEANING |
|---|---|
== | Checks is two types are equal, not very strict. "5"==5 will also give true |
=== | Checks is two variables are equal even their types. "5"===5 is false. |
!= | Not Equal |
!== | Strict Not Equal |
> | Greater than |
< | Less than |
>= | Greater or equal |
<= | Less or equal |
let us see them in action !
console.log("5"==5) //true
console.log("5"===5) //false
console.log(8!="8") //false
console.log(8!=="8") //true
console.log(6>3) //true
console.log(6<3) //false
console.log(3>=3) //true
console.log(4<=5) //true
Logical Opeators
Logical operators (&&, ||, !) are used to combine or manipulate boolean values. They help control decision-making by evaluating multiple conditions together.
OPERATOR | MEANING |
|---|---|
&& | AND, it returns true only if both LHS and RHS of the operator are true, otherwise false. |
|| | OR, it return true if any side of it is true, returns false only if both sides appear false. |
! | NOT, it reverts the boolean value obtained |
Let us get confident through code !
(5>2) && (6<10) // true && true => true
(5>=5) && (1<0) // true && false => false
(5<2) || (6>10) // false || false => false
(5>=5) || (1<0) // true || false => true
!(5<3) // !false => true
Unary Operator
Unary operators operate on a single operand and perform actions like incrementing, negating, or checking type. Examples include typeof, ++, and --.
let x = 5;
++x; // 6 (increment)
typeof x; // "number" (type checking)
--x; // 5 (decrement)
Unary ++ and -- could be used both as prefix and postfix and yes they perform differently.
let x = 5;
let a = ++x; // Prefix
// x becomes 6 first
// then a gets 6
let y = 5;
let b = y++; // Postfix
// b gets 5 first
// then y becomes 6
++x→ Increment first, then use the valuex++→ Use the value first, then increment
So prefix changes the value before assignment, postfix changes it after.
Ternary Operator
You must be aware of if-else statements, well there appears a shortcut way to assign value, which you would be doing using if-else. First we see an if-else example.
let a;
if(5>4){
a=10;
} else {
a=20;
}
let a= 5>4?10:20 //condition ? value_if_true : value_if_false
Conclusion
Operators are fun ! You need not memorize them to understand them, just use them as you continue your coding journey and you shall get a hang of it!


