A Guide To Operators In JavaScript

An operator performs operation on one or multiple values and build result. There are 5 categories of operators.

  • Comparison Operators
  • Arithmetic Operators
  • Assignment Operators
  • Conditional Operators
  • Logical Operators

Comparison Operators

This list of operators always requires two operands and returns a boolean value as the result.

OperatorsDescription
==equal to
===matches value and type
!=not equal to
>greater than
>=greater than or equal to
<less than
<=less than or equal to
var name = "Jon", new_name = "Jon";

name == new_name //returns true
name === new_name //returns true
name != new_name; //returns false

var i = 10, j = 20 ;

i > j //returns false
j >= i //return true
i < j //returns true
i <= j //returns true

Arithmetic Operators

This list of operators performs the arithmetic operations on provided data.

OperatorsDescription
+Addition
Subtraction
*Multiplication
/Division
%Modulus operator. returns the remainder.
++Increment operand by 1
Decrement operand by 1
var i = 10, j = 20 ;

i + j; //returns 30
j - i; //returns 10
i * j; //returns 200
j / i; //returns 2
i % j; //returns 0
j++; //returns 21
i-- ; //returns 9 

Assignment Operators

This list of operators assigns value to the left operand by performing arithmetic operations on the right operand.

OperatorsDescription
=Provides the value of the right operand to the left oeprand
+=Acts as addition between left and right operand
-=Acts as subtraction between left and right operand
*=Acts as multiplication between left and right operand
/=Divides the left operand with the right operand
%=Modulus of the Division of the left operand with the right operand
var i = 10, j = 20 ;

i = j ; //returns 20
j += i; //returns 30
i *= j; //returns 200
j /= i; //returns 2
i %= j; //returns 0

Conditional Operators

This list of operators assigns value to the variable based on a condition provided. It is also called as Ternary Operator

OperatorsDescription
?: condition ? value1 : value2
var i = 10, j = 20 ;

var k = i < j ? i : j; //k will be 10

Logical Operators

This list of operators used for joining two or more condition.

OperatorsDescription
&&It is an AND operator. It checks whether all the provided conditions are non zero. If it is non-zero then returns true or else will return false
||It is an OR operator. It checks whether at least one condition is non-zero then returns true or else will return 0.
!It reverses the result of the operand or condition
var i = 10, j = 20 ;

(i == j) && (i > j) //returns false
(i < j) || (i == j) //returns true
!(i > j) //returns true

I hope this post was helpful to you. If you like my post, follow me @twitter/andramazo.

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top