Skip to main content

JavaScript Operators

Operators is an special symbols or characters, which are used to compare, assignment, and perform logical and mathematical operation. That is a simple way to describe the role of an operator. But that is a basic level, operator is much more powerful. That are used basic and special purpose.

Generally operator are involved of many place in our code, Such as when, define a similar type of multiple variable then there are use comma operator to separating the variable. Dot operator are used to method of class and there are in similar way many operator are used to perform some specific task.

In this post are given examples which are commonly used to mathematical and logical operation.

Arithmetic Operator

Arithmetic Operator are work in two operands, that is also know as binary operators. We can thinking about this type of operators. This operator are works two operands such as left and right. And operator are exist in between of them. operator is decide to which operation are performed to those operand. In given table mention of all arithmetic operators.

Operator Name
+ Addition(+)
- Subtract (-)
* Multiplication (*)
/ Divide
% Remainder operator
** Exponent (power)

Basically arithmetic operator are used to perform 6 type of operation. compiler are know about what is the role of symbol that are applied to operands (variable value or constant literals).

var a = 10; 
var b = 2;

console.log( ` a : ${a} , b : ${b}`);

console.log( ` (${a} +  ${b}) =  `,a+b);
console.log( ` (${a} -  ${b}) =  `,a-b);
console.log( ` (${a} *  ${b}) =  `,a*b);
console.log( ` (${a} /  ${b}) =  `,a/b); 
console.log( ` (${a} %  ${b}) =  `,a%b);
console.log( ` (${a} **  ${b}) = `,a**b);

Output

 a : 10 , b : 2
 (10 +  2) =   12
 (10 -  2) =   8
 (10 *  2) =   20
 (10 /  2) =   5
 (10 %  2) =   0
 (10 **  2) =  100

In JavaScript, (+) addition operator are used to adding the numeric value. (-) operator are used to Subtract two values, (*) operator are used to multiplication of two values. and similarly (/) operator are divide the two values and (%) are used to get remainder of two values. Modulus % operator are work only integer values.

Logical Operator

Logical operator are work in boolean values. && operator are used to check expression condition when both left and right the expression are produced boolean true value. Then that are produce an true boolean result. Otherwise produce false. In given list are mentioned logical operators.

Operator Name
&& Logical AND operator.
|| Logical OR Operator
! Logical NOT Operator

Example of logical AND operator.

var result = true && true;
console.log(`${result}`);   //true

result = false && false;
console.log(`${result}`);  //false

result = true && false;
console.log(`${result}`);  //false

result = false && true;
console.log(`${result}`);  //false

Output

true
false
false
false

Logical Or (||) is an binary operator, That is work on boolean values. This operator are produce true value when any one expression are produced true result (left and right operand result).

var result = true|| true;
console.log(`${result}`);   //true

result = false|| false;
console.log(`${result}`);  //false

result = true|| false;
console.log(`${result}`);  //true

result = false|| true;
console.log(`${result}`);  //true

Output

true
false
true
true

Logical Not (!) is an unary operator that are work on boolean value. when expression is produce true value that is modified this result to false. and when expression is produce false boolean value then that is change this result to true boolean value.

var trueStatus = true;
var falseStatus = !trueStatus; //use logical not
console.log(`trueStatus :${trueStatus}`); 

console.log(`falseStatus : ${falseStatus}`);

Output

trueStatus :true
falseStatus : false

We can also use this operator to check logical condition result. The main role of Logical Not operator is inverts the boolean result. And this is prefix operator.

if(!(5 > 15)){
    // (5>15) = false =>  !false = true

	console.log(`Five is not greater than 15`); 

}

Output

Five is not greater than 15

Bitwise Operators

Bitwise operator is based on binary value and that are work on two operands. Normally all operators are produce boolean values but that is work on a binary bit level modification. In given table mentioned all Bitwise operator.

Operator Name
& Binary AND operator.
| Binary OR Operator
^ Binary XOR Operator
~ Binary Ones Complement Operator
<< Binary Left Shift Operator
>> Binary Right Shift Operator

Binary AND operator

/*
  Example Binary And Operator
/*
*  Binary of 5 =   0  1  0   1
*  Binary of 4 =   0  1  0   0
*            &
*--------------------------------
*     Result 4 =   0  1  0   0

*/

var a = 5 & 4;
console.log(a); //4

Output

4

Binary Or operator

/*

  Example Binary OR Operator

/*
*  Binary of 3 =   0  0  1   1
*  Binary of 4 =   0  1  0   0
*            |
*--------------------------------
*     Result 7 =   0  1  1   1
*/
var a = 3 | 4;

console.log(a); //7

Output

7

Binary XOR operator

/*
Example Binary XOR Operator

*  Binary of 5 =   0  1  0   1
*  Binary of 4 =   0  1  0   0
             ^   
*--------------------------------
*     Result 1 =   0  0  0   1

    When  0  1  = 1
    When  0  0  = 0
    When  1  1  = 0
*/

var a = 5 ^ 4;
console.log(a); //1

Output

1

Binary Ones Complement Operator

var data1=15;
var data2 = -6;

  /*Bitwise 1s Complement Operator
    Example 1:

      15  = 1  1  1  1 (binary of 15)
      add            1 (binary of 1)
    +   
  --------------------------------
  Result= 1  1  1  1 (binary of 16)
  --------------------------------- 
    output= - Result
    output= - 16

    Example 2: first remove that (- sign)
      6   = 0  1  1  0 (binary of  6)
      minus          1 (binary of  1)
    -   
  --------------------------------
  Result= 0  1  0  1 (binary of 5)
  Result= 5 
  //add remove sign
  Result =-5 
  --------------------------------- 
    //multiply by (- sign)
    output= - Result
    output= - (-5)
    output= 5

  */

console.log(~data1);
console.log(~data2);

Output

-16
5

Binary Left Shift Operator

var data1 = 15;
var data2 = 2;

  /*Bitwise <<
    Example 1:

      15        = 0  0  1  1  1  1 (binary of 15)
  left shift 2  = 1  1  1  1  0  0  
      
  --------------------------------
   60     =     1  1  1  1  0  0 
  ---------------------------------
  Or
  Formula   = data * (2^ left shift)
  example 1 =  15   *(2^2)  2 bit shift
            =  15*4
            =  60
  example 2 =  2 *(2^5) 
        =  2 *(32)
        =  64          
  */

console.log(15 << 2); //60
console.log(2 << 5);  //64

Output

60
64

Binary Right Shift Operator

var data1=15;
var data2=2;
/*Bitwise >>
Example 1:

  15          = 1  1  1  1 (binary of 15)
right shift 2 = 0  0  1  1  
  
--------------------------------
3     =        0  0  1   1
---------------------------------     
*/

console.log(15>>2); //3

Output

3

Relational Operators

Relational operator are work on two operands, and normally that are used to compare operands value. This operator are produce boolean result. And which are normally used in conditional statement to check expression.

Operator Name
> greater than
< less than
== equal to equal to
>= greater than equal to
<= less than equal to
!= Not equal to
var a = 5;
var b = 7;

console.log(` a : ${a} , b : ${b}`);

console.log(` (${a} >  ${b}) = ${a>b}`);
console.log(` (${a} <  ${b}) = ${a=  ${b}) = ${a>=b}`);
console.log(` (${a} <=  ${b}) = ${a<=b}`); 
console.log(` (${a} !=  ${b}) = ${a!=b}`);
 a : 5 , b : 7
 (5 >  7) = false
 (5 <  7) = true
 (5 ==  7) = false
 (5 >=  7) = false
 (5 <=  7) = true
 (5 !=  7) = true

Assignment Operators

Assignment operator are used to assign a value of right hand side to left hand variables. In given table are view short form of assignment operator. Which are perform some operation of two operand and result are store in the left side variable.

Operator Meaning
*= Multiply and assign
+= Add and assign
/= Divide and assign
-= Subtract and assign
%= calculate remainder and assign
= Equal

Basically normal (=) assignment operator are used to set value of variable. This are assign value according to data type of variable. But short form we are combine this operator and arithmetic operator and perform two type of operation. First are performed arithmetic operation and resultant are store by assignment operator.

Ternary Conditional Operator

This operators is similar to C Ternary operator. There is divided into three section, first one are expression. If result of first expression result is true then work on second section expression. And if first expression is false then it will executing on third section expression.

//section 1             section 2    section 3
expression/condition ?     Pass   :   Fail 
var a = 10 

var result = (a%2==0) ? "Even number" : "Odd Number";

console.log(result); //Even number




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment