JavaScript Operators are special characters that can be used to control the flow of the code. They’re similar to operators found in other programming languages, and they come in handy for things like conditionals and loops.
In this post, we’ll introduce you to the most common operators used in JavaScript and teach you how to use them. Then, we’ll show you some examples of how they can be used in code. So read on, and learn all about JavaScript operators.
Table of Contents
What is Operator?
Operators in programming language are used to perform simple mathematical or logical operations on data. They can be classified according to the kind of operation they perform, By understanding which operator is used for specific operations, you can easily write code that is easy to understand and remember.
Operators also play an important role in error handling and debugging. Additionally, if there’s something wrong with your code when it’s run-time, operators can be used along with conditional statements to determine which lines should be executed based on certain conditions being met.
Types of Operators
1.Arithmetic Operator
Arithmetic operators are the basic operations that you use to perform mathematical calculations in JavaScript. You can specify these operators as either function calls or method names, depending on what is more developer-friendly for you.
Operator | Name | Example |
---|---|---|
+ | Addition | a+b |
– | Subtraction | a-b |
/ | Division | a/b |
* | Multiplication | a*b |
% | Reminder | a%b |
++ | Increment | a++ |
— | Decrement | a– |
** | Exponentiation | a**b |
const addition = 6 + 5
const subtraction = 6 - 5
const division = 6 / 5
const multiplication = 6 + 5
const reminder = 6 % 5
let x = 3;
const y = x++;
let a = 3;
const b = a--;
console.log("addition is :",addition)
console.log("subtraction is:",subtraction)
console.log("division is:",division)
console.log("multiplication is:",multiplication)
console.log("reminder is:",reminder)
console.log(`x:${x}, y:${y}`); // increment
console.log(`x:${x}, y:${y}`); // decrement
Output

2.Assignment Operator
Assignment operator is a reserved keyword in JavaScript that allows you to assign values to variables. It takes the form of =, and can be used within strings, numbers, objects, or functions.
Operator | Name | Example |
---|---|---|
= | Assignment Operator | a=8 |
+= | Addition Assignment | a+=8 |
-= | Subtraction Assignment | a-=8 |
/= | Division Assignment | a/=8 |
*= | Multiplication Assignment | a*=8 |
%= | Reminder Assignment | a%=8 |
++= | Increment Assignment | a++=8 |
–= | Decrement Assignment | a–=8 |
**= | Exponentiation Assignment | a**=8 |
const x = 9 // assignment op
const x+=5 // addition assignment
const x-=5 //subtraction assignment
const x/=5 //division assign
const x*=7 //multiplication assign
const x++= 9//increment assign
const x--= 10 //decrement assign
const x**= 11 // exponentiation assign
3.Comparison Operator
The comparison operator in JavaScript is the != (not equal) symbol. It can be used to compare two expressions and return a value that indicates whether or not they are not equal.
Operator | Name | Example |
---|---|---|
== | equal to //returns true if operand are equal | a=8 |
!= | not equal to //returns true if operands are not equal | a+=8 |
=== | Strict equal // true if operand are equal and of same type | a-=8 |
!== | Strict not equal //true if operand are equal but of different type | a/=8 |
> | Greater than / true if left operand is greater than right operand | a*=8 |
>= | Greater than equal to // true if left operand is greater than right or both are equal | a%=8 |
< | less than // true if right operand is greater than left operand | a++=8 |
<= | Less than equal to / true if right operand is greater than left operand | a–=8 |
// equal operator
console.log(9 == 9); // true
// not equal operator
console.log('codeleaks' != 'codeLeaks'); // true
// strict equal operator
console.log(9 === '9'); // false
// strict not equal operator
console.log(9 !== 2); // false
Output

4.Logical Operator
The Logical operator is a special type of mathematical operator that allows two conditions to be tested simultaneously. These operators can be used in conjunction with other statements, such as if statements or looping constructs.
Operator | Name | Example |
---|---|---|
&& | Logical AND // true if both Operands are true else false | a && b |
|| | Logical OR if both operand are false returns false else true | a || b |
! | Logical NOT if operand is false returns true and vice versa | !a |
// logical AND Operator
console.log(true && true);
console.log(true && false);
// logical OR Operator
console.log(true || false);
// logical NOT Operator
console.log(!true);
Output

5.Bitwise Operator
The Bitwise operator is a function that allows you to perform logic operations on binary data. It can be used in conjunction with the logical AND and OR operators, so it enables you to manipulate bits within a binary number.
Operator | Name |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | left shift |
>> | Sign-propagating right shift |
>>> | zero fill right shift |
6.String Operator
String operator in javascript is a function that takes two strings as input and returns another string concatenation of the two inputs. For example, “str” + “operator” will return the string “operatorstr”.
console.log('welcome' + ' to codeleaks');
let a = 'learn';
a += ' javascript';
console.log(a);
Output

7.Ternary Operators
A ternary operator is a special type of if-else condition statement that allows you to use three different values in a single expression. This can be useful for things like determining the value of a variable based on one or more conditions.
var a = 10, x = 5;
var y = a > x? a : x;
var z = a > x? x : a;
console.log(y)
console.log(z)
Output

8.Typeof Operator
Typeof operator allows you to restrict the type of data that is returned by a function. For example, if you have a function that returns an object, you can use Typeofoperator to determine whether the object’s properties are strings or numbers.
var x = "welcome to codeleaks";
console.log("Type of x = " + (typeof x));
Output

9.Conditional Operator
Conditional Operator is a operators that allows you to make decisions based on certain conditions. It can be used with boolean expressions and if statements.
10.Special Operator
operator | Name | Example |
---|---|---|
, | evaluate multiple operands and return the value of last operand | var x= 2,3,5; |
?, | return values based on conditions | (2 > 3) ? ‘success’ : ‘fail’; |
delete | delete element of array or object property | delete x |
void | discard the expression return value | void (x) |
in | returns true if the specific property is present in object | prop in object |
instanceof | discard the expression return value | object instanceof obect_type |
Also learn how to write the basic syntax of javascript to code efficiently.
FAQs
Which operator Cannot overload?
If the dot (.) operator is overloaded, it will produce an error.
Which operator has the highest precedence?
Certain operators, such as multiplication and addition, have a higher precedence than others. Operators with the highest precedence are at the top of the table, while those with the lowest precedence are at the bottom.
Conclusion
Now that you have seen how JavaScript operators work and what they do, it is time to use them in your code and see the result. After all, they are just tools to write efficient code that’s easy to read. Sometimes, it can be hard to keep track of which operator suits the task at hand because they are so simple! However, knowing the basics can help you write powerful JavaScript code in no time!