JavaScript

List Of JavaScript Operators

JavaScript provides different type of operations same as other programming languages. There are some additional operators which make javascript more suitable for programming.

Javascript supports the following types of operators:

1) Arithmetic Operators

They are used to perform different arithmetic operations between two or more variables.

Operator Description Example: (here m = 8, n = 2 )
+ Addition var x = m + n   // Ans: 10
Subtraction var x = m – n // Ans: 6
* Multiplication var x = m * n    // Ans: 16
/ Division var x = m / n // Ans: 4
% Modulus var x = m % n    // Ans: 0

2) Logical Operators

They are used to perform logical operations like AND Operation, OR Operation and NOT operations between two or more variables.

Operator Description Example: (here m = 8, n = 2 )
&& AND var x = (m > 0) && (n < 5)    // Ans: true
|| OR var x = (m == 8) || (n < 0)    // Ans: true
! NOT var x = !(n == m – 6)    // Ans: false

NOTE:

  • AND operation is true if all conditions are true otherwise it is false.
  • OR operation is false if both conditions are false otherwise it is true.
  • NOT operation returns opposite result of a condition.
  • Logical operations are used to get result for given conditions and based on that result further actions are taken.

3) Comparison Operators

They are created to compare two or more variables. They are mostly used with logical operators.

Operator Description Example: (here m = 8, n = 2 , int)
> greater than var x = m > n    // Ans: true
< less than var x = m < 5    // Ans: false
>= Greater than Or Equal to var x = m >= n    // Ans: true
<= less than Or equal to var x = n <= 2    // Ans: true
== Equal to var x = (m == n)    // Ans: false
=== Equal to with Equal type var x = (m === 8)    // Ans: true
!= Not Equal to var x = m != n    // Ans: true
!== Not Equal to with Not Equal type var x = m !== 5    // Ans: true

4) Increment – Decrement Operator

They are used to increment and decrement the values.

Operator Description
++ Increment
Decrement

Note: Based on the position they are used, we can divide them into two types.

1) Pre-increment / Post-increment

Example: (m =8 , n = 2)
Pre increment
var x = m++; // Ans: x = 8 , m = 9
Post increment
var x = ++n // Ans: x = 3 , n = 3

2) Pre decrement / Post decrement

Example: (m =8 , n = 2)
Pre decrement
var x = m–; // Ans: x = 8 , m = 7
Post decrement
var x = –n // Ans: x = 1 , n = 1

5) Conditional Operator

They are also called ternary operators. According to the condition, the result value is assigned to a variable.

Syntax:

Variable name = ( condition ) ? val1 : val2

Explanation:

If the condition is true, then val1 is assigned to the variable; otherwise, val2 is assigned to the variable.

Example: (m =8 , n = 2)
var x = (m > 5) ? 7 : 3 // Ans: x = 7
var x = (n == 5) ? 7 : 3 // Ans: x = 3

6) Bitwise Operator

JavaScript supports different bitwise operators. During these operations, data is converted into a 32-bit format, and then operations are performed, again the result is converted back to javascript number.

Operator Description Example Same as Result Decimal
& AND x = 4 & 1 0100 & 0001 0000 0
| OR x = 4 | 1 0100 | 0001 0101 5
~ NOT x = ~ 4 ~0100 1011 11
^ XOR x = 4 ^ 1 0100 ^ 0001 0101 5
<< Left shift x = 4 << 1 0100 << 1 1000 8
>> Right shift x = 4 >> 1 0100 >> 1 0010 2

7) String Operator

They are used to concatenate two or more strings in javascript.

Example 1:

var e1 = “I am”;
var e2 = “ Errorsea”;
var e3 = e1 + e2; // Ans: “I am Errorsea”

Example 2:

var e1 = “I am”;
var e2 = “ Errorsea”;
var e1 += e2; // Ans: “I am Errorsea”

8) The type of Operator

It returns the type of a variable.

Example:

type of “Errorsea” // Ans: string
typeof 200 // Ans: number
typeof NaN // Ans: number
typeof true // Ans: boolean
typeof [1, 2, 4, 7] // Ans: object
typeof {name:’Errorsea’, age:18} // Ans: object
typeof new Date() // Ans: object
typeof null // Ans: object
typeof Bike // Ans: undefined (if Bike is not declared)
typeof function () {} // Ans: function

9) Delete Operator

It used to delete properties of objects in javascript.

Example:

var person = {firstName:”Nick”, age:18, Color:”white”};
delete person[“Color”];

The above code deletes the Color property from the object person.

Note: We cannot use the deleted property in the code without adding it again.

10) The in Operator

It is used to check the specified property in the object.
This operator returns true if the specified property is present in the object.

Example:

var person = {firstName:”Nick”, age:18, Color:”white”};
var x = age in person; // Ans: true
var x = Birthdate in person; //Ans: false

11) The instance of Operator

It is used for checking whether the specified object is an instance of in the object or not. This operator returns true if the specified property is an instance of the object.

Example:

var media = [“facebook”, “whatsapp”, “instagram”];
media instanceof Array; // Ans: true
media instanceof String; // Ans: false
media instanceof Object; // Ans: true
media instanceof Number; // Ans: false

12) The void Operator

This operator is mostly used to get undefined primitive value using void(0) in javascript. It evaluates the expression and returns undefined.

Example:

<a href=”javascript:void(0)”>I am useless link</a>

13) Assignment Operator

It is used to assign value to the Javascript variable.
It is denoted by ‘ = ’ sign.

Example:

var m = “Errorsea”;
var n = 5;

Note: Javascript also supports shorthand notations with assignment operators.

Types of Assignment Operators

Operations Description Same as (here m = 8, n = 2 )
m = n assignment m = n // Ans: m=2
m += n Addition m = m + n // Ans: m=10
m -= n Subtraction m = m – n // Ans: m=6
m *= n Multiplication m = m * n // Ans: m=16
m /= n Division m = m / n // Ans: m=4
m %= n Modulus m = m % n // Ans: m=0

Conclusion

These are the different types of operators used in javascript according to the requirements for providing users the better facilities. User can take actions according to the result of different operators.

Yash Panchal

Computer science student, Working as Web Developer and part-time Blogger. Highly interested in AI & ML.

Recent Posts

5 Important Things To Know About WordPress Before You Use It

There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…

3 years ago

How to Install MySQL on Your PC in 3 Easy Steps

In this tutorial, I'm going to show you how to install MySQL on your computer.…

5 years ago

Download and Install Turbo C++ for Windows 10 (Full Installation Guide)

Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…

5 years ago

PHP .HTACCESS Redirects

We can redirect any webpage to any other or redirect the whole domain or website…

5 years ago

PHP Redirect Pages

There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…

5 years ago

PHP Include & Required

Include files in PHP are used in appending various global or config files. We can…

5 years ago