JavaScript Operators

javascript-operators

Learning JavaScript operators is quite essential for you if you want to work with the JavaScript programming language. This tutorial talks about different types of JavaScript operators.

What are JavaScript Operators?

JavaScript operators are the collection of operators or symbols that perform certain operations on the operands. In any JavaScript program, operators play a major role in manipulating operands by exercising a wide range of operations.

Additionally, there are several different types of JavaScript operators that you should be aware of.

Examples:

var addition = 10 + 5; // Here “+” is the operator, and “10” and “5” are operands

var subtraction = 10 – 5; //Here “-” is the operator, and “10” and “5” are operands

Types of JavaScript Operators

There are six types of JavaScript operators that are as follows:

  1. Assignment Operators
  2. Arithmetic Operators
  3. Logical Operators
  4. Comparison Operators
  5. Bitwise Operators
  6. Special Operators

Let us now discuss each type of JavaScript operator in detail.

1. Assignment Operators

With assignment operators, we can assign or manipulate the values of operands. The following table lists all the assignment operators:

OperatorDescriptionExample
=Assignvar x = 10; 
+=Add and Assignvar x=10;

 

x+=5; // x = x + 5 i.e. x = 15

-=Subtract and Assignvar x=10;

 

x-=5; // x = x – 5 i.e. x = 5

*=Multiply and Assignvar x=10;

 

x*=5; // x = x * 5 i.e. x = 50

/=Divide and Assignvar x=10;

 

x/=5; // x = x / 5 i.e. x = 2

%=Modulus and Assignvar x=10;

 

x%=5; // x = x % 5 i.e. x = 0 (Remainder)

**=Exponentiate and Assignvar x=10;

 

x**=2; // x = x ** 2 i.e. x = 100 

2. Arithmetic Operators

With Arithmetics operators, we can perform various arithmetic operations on operands. The following table lists all the arithmetic operators in JavaScript:

OperatorDescriptionExample
+Additionvar x = 10 + 5;
Subtractionvar x = 10 – 5;
*Multiplicationvar x = 10 * 5;
/Divisionvar x = 10 / 5;
%Modulusvar x = 10 % 5;
++Incrementvar x = 10;

 

x++; // x = 11

Decrementvar x = 10;

 

x–; // x = 9

**Exponentiationvar x = 10**2; // x=100

3. Logical Operators

With Logical operators, we can perform various logical operations. The output of a logical operation is a Boolean value, i.e., true or false. The following table highlights all the logical operators in JavaScript:

OperatorDescriptionExample
&&Logical AND(5==10 && 10==10); // returns true
||Logical OR(5==10 || 10==10); // returns false
!Logical NOT!(5==10); // returns true

4. Comparison Operators

With Comparison operators, we can compare two operands, and these operators are of great importance for implementing loops. The following table consists of all the comparison operators in JavaScript:

OperatorDescriptionExample
==Equal to10==10; // returns true
!=Not equal to10!=10; // returns false
===Strictly equal to (compares value and type of operands, and both must be true)10===”10″; // returns false
!==Strictly not equal to (compares value and type of operands, and any parameter can be false)10!==”10″; // returns true
>Greater than10>20; // returns false
>=Greater than or equal to20>=10; // returns true
<Less than10<20; // returns true
<=Less than or equal to20<=10; // returns false

5. Bitwise Operators

With Bitwise operators, we can perform various bitwise operations on operands. Also, bitwise operators work with 32-bit numbers, and thus during a bitwise operation, each number is converted into its corresponding 32-bit number. Also, after the operation is completed, the result, which is a 32-bit number, is converted back into decimal form. The following  table lists all the bitwise operators in JavaScript:

OperatorDescriptionExample 
&Bitwise AND3 & 9; // returns 1
|Bitwise OR3 | 9; // returns 11
^Bitwise XOR3 ^ 9; // returns 10
~Bitwise NOT~3; // returns -4
<<Bitwise Left Shift3 << 9; // returns 1536 
>>Bitwise Right Shift3 >> 9; // returns 0
>>> Bitwise Right Shift with Zero3>>>9; // returns 0

6. Special Operators

There are several special operators in JavaScript that you may need to use in certain scenarios. The following table highlights various special operators in  JavaScript:

OperatorDescriptionExample
,It evaluates multiple expressions or operands and returns the last operandx = (3, 9); // returns 9
?:It is like the if-else statement that returns one of the two values depending on the specified condition(5<10) ? “true”:”false”); // returns true
typeofChecks and returns the type of an operandtypeof (3); // returns number
instanceofChecks if the specified object is of a particular object typevar name = new String(“John”);

 

name instanceof String; // returns true

deleteDeletes an element of an array or a property of an objectvar array = [1,2,3,4];

 

delete array[3];

const Fullname = {

firstName: ‘John’,

lastName: ‘Doe’

};

delete Employee.firstName;

inChecks if the specified property is in an objectconst Employee = {

 

    firstname: ‘John’,

    lastname: ‘Doe’

  };

‘firstname’ in Employee; // returns true

voidDiscards the return value of an expression.var x = 5;

 

void (x); // returns undefined

Takeaway

As a JavaScript developer, it is essential for you to become familiar with all the important JavaScript operators. At times, when you write JavaScript code or programs, you need to perform various operations on variables or operands to make sure that the overall code or program behaves as intended.

People Who Read This Article Also Read: