Operators play a vital role in any programming language. Operators are used extensively to build applications like the calculator app, where mathematical computations are necessary. They are also used to perform logical comparisons and execute the required task in a software application.
In this blog, we will discuss operators in Javascript like arithmetic operators, comparison operators, logical operators, etc., in detail.
Operators are symbols used for performing mathematical operations, logical comparisons. An operand (or argument) is an object or quantity on which the operator is applied. For instance, in "1 + 2", 1 and 2 are the operands, and "+" is the operator.
Below are the following types of operators in JavaScript.
They are used to compare the two operands, and the result is always true or false. Let's go through the list of comparison operators in JavaScript.
Operator
Description
==
Equal to operator returns the value true if the operands are equal.
===
Strict equal to operator returns the value true if the operands are equal and of the same type.
!=
Not equal to operator returns the value true if the operands are not equal.
!==
Strict not equal to operator returns the value true if the operands are not equal or are of different types.
>
Greater than operator returns the value true if the value of the left operand is greater than the value of the right operand.
>=
Greater than or equal to operator returns the value true if the value of the left operand is greater than or equal to the value of the right operand.
<
Lesser than operator returns the value true if the left operand is smaller than the right operand.
<=
Lesser than or equal to operator returns the value true if the left operand is smaller than or equal to the right operand.
Example
let val1 = 10;
let val2 = 5;
let val3 = "10";
let val4 = 15;
// equal to operator
console.log("val1 == val3: " + (val1 == val3));
// strict equal to operator
console.log("val1 === val3: " + (val1 === val3));
// not equal to operator
console.log("val1 != val2: " + (val1 != val2));
// strict not equal to operator
console.log("val1 !== val3: " + (val1 !== val3));
// greater than operator
console.log("val1 > val2: " + (val1 > val2));
// greater than or equal operator
console.log("val1 >= val4: " + (val1 >= val4));
// lesser than operator
console.log("val1 < val4: " + (val1 < val4));
// lesser than or equal operator
console.log("val1 <= val2: " + (val1 <= val2));
Output
val1 == val3: true
val1 === val3: false
val1 != val2: true
val1 !== val3: true
val1 > val2: true
val1 >= val4: false
val1 < val4: true
val1 <= val2: false
Bitwise operators in JavaScript
They are used to perform Bitwiseoperations on operands. Let's go through the list of bitwise operators in JavaScript.
Operator
Description
&
Bitwise AND operator returns "1" in each bit position for which the corresponding bits of both operands are "1". Else returns "0".
|
Bitwise OR operator returns a "0" in each bit position for which the corresponding bits of both operands are "0". Else "1" is returned.
^
Bitwise XOR operator returns a "0" in each bit position for which the corresponding bits of both operands are the same. Else "1" is returned.
~
Bitwise NOT operator inverts the bits of its operand.
<<
Bitwise left shift operator shifts all the bits present in the first operand to the left by the number specified in the second operand.
>>
Bitwise right shift operator shifts all the bits present in the first operand to the right by the number specified in the second operand.
<<<
Bitwise zero-fill right shift operator shifts all the bits present in the first operand to the right by the number specified in the second operand. It discards bits shifted off and shifts in zeros from the left.
Example
let num1 = 0;
let num2 = 1;
// bitwise AND operator
console.log("num1 & num2:" + (num1 & num2));
// bitwise OR operator
console.log("num1 | num2:" + (num1 | num2));
// bitwise XOR operator
console.log("num1 ^ num2:" + (num1 ^ num2));
// bitwise NOT operator
console.log("~num1:" + (~num1));
// bitwise left shift operator
console.log("num2<<1: " + (num2<<1));
// bitwise right shift operator
console.log("num2>>1: " + (num2>>1));
// bitwise zero-fill right shift operator
console.log("num2>>>1: " + (num2>>>1));
Logical operations like AND, OR, and NOT are performed on the operands using the logical operators. Let's go through the list of logical operators in JavaScript.
Operator
Description
&&
Logical AND returns the value true if both the operands/boolean values are true. Else false is returned.
||
Logical OR returns the value true if at least one of the operands/boolean values are true. Else false is returned.
!
Logical NOT return the value true if the operand is false and vice-versa.
Example
let val1 = true;
let val2 = false;
// logical and operator
console.log("val1 && val2: "+ (val1 && val2));
// logical or operator
console.log("val1 || val2: "+ (val1 || val2));
// logical not operator
console.log("!val1: "+ (!val1));
Output
val1 && val2: false
val1 || val2: true
!val1: false
Assignment operators in JavaScript
They are used to assign values to the operand. Let's go through some of the assignment operators in JavaScript.
Operator
Description
=
It assigns values from the right side operand to the left side operand.
+=
It adds the right operand to the left operand and assigns the resultant value to the left operand.
-=
It subtracts the right operand from the left operand and assigns the resultant value to the left operand.
*=
It multiplies the right operand with the left operand and assigns the resultant value to the left operand.
/=
It divides the left operand with the right operand and assigns the resultant value to the left operand.
%=
It takes the modulus using two operands and assigns the result to the left operand.
On strings apart from the comparison operators, the concatenation operator (+) can be used. The concatenation operator concatenates two string values together and returns a new string.
The comma operator "," evaluates the operands and returns the value of the last operand. It is mainly used inside a for loop to allow multiple variables to be updated each time through the loop.
Example
// a is assigned with the last value
let a = (1, 2, 3);
console.log("a: " + a);
let arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]
console.log("Array before updation: ");
for(let i = 0 ; i < 3 ; i++)
console.log(arr[i]);
// comma operator to updates two variables at once
for (let i = 0, j = 0; i < 3; i++, j++)
arr[i][j] += 1;
console.log("Array after left diagonal elements are updated by 1: ");
for(let i = 0 ; i < 3 ; i++)
console.log(arr[i]);
Output
a: 3
Array before updation:
[ 1, 2, 3 ]
[ 4, 5, 6 ]
[ 7, 8, 9 ]
Array after left diagonal elements are updated by 1:
[ 2, 2, 3 ]
[ 4, 6, 6 ]
[ 7, 8, 10 ]
Operator Precedence
The execution order is defined by the operator precedence order when an expression has more than one operator. Operator precedence can be overridden using parentheses.
The following table describes the operator precedence order, from highest to lowest.
Q. What is the logical operator for or JavaScript?
In Javascript, the logical OR is represented as || operator. The OR operator returns true if any one of its operands is true and false if both the operands are false.
Q. What is the === operator in JavaScript?
=== Operator is used for the comparison. The strict equality "===" is used to compare the value and type of the operands. It checks for both the value as well as the data type for the operands.
Q. What does || mean in coding?
|| is a Logical OR operator. This operator returns a boolean value. It returns true if any one of the operands is true and false when both the operands are false.
Q. How to add 2 conditions in if statement in JavaScript?
To add two conditions in an if statement in JavaScript, you can make use of OR(||), AND(&&) or Not(!) operators. These operators helps in defining two conditions together in an if statement.
Conclusion
The blog covered the various operators in JavaScript, namely, arithmetic operators, comparison operators, bitwise operators, logical operators, assignment operators, string operators, and the comma operator. The precedence order of the operators has also been covered. Now that you know the concept of variables and data types in Javascript, go ahead and try out some questions based on them on our Coding Ninjas Studio Platform! To learn more about Micro Operations, refer to Arithmetic Micro Operations.