JavaScript Operators Tutorial

JavaScript Operators

An operator performs some operation on single or multiple operands (data value) and produces a result. For example 1 + 2, where the + sign is an operator 1 is a left operand and 2 is a right operand. The + operator adds two numeric values and produces a result which is 3 in this case.

JavaScript includes the following categories of operators.

  1. Arithmetic Operators
  2. Comparison Operators
  3. Logical Operators
  4. Assignment Operators
  5. Conditional Operators

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations. Below are some arithmetic operators with a short description.

OperatorDivide the left operand by right operand.
+Adds two numeric operands.
Subtract the right operand from the left operand
*Multiply two numeric operands.
/Modulus operator. Returns the remainder of two operands.
%Divide the left operand by the right operand.
++Increment operator. Increase operand value by one.
Decrement operator. Decrease value by one.

Example: Arithmetic Operators 

var x = 6, y = 12, z = 15;

x + y; //returns 18

y - x; //returns 6

x * y; //returns 72

y / x; //returns 2

x % 2; //returns 0

x++; //returns 7

x--; //returns 5

Comparison Operators

JavaScript language includes operators that compare two operands and return the Boolean value true or false.

OperatorsDescription
==Compares the equality of two operands without considering type.
===Compares equality of two operands with type.
!=Compares inequality of two operands.
>Check whether left side value is greater than the right side value. If yes then returns true otherwise false.
<Check whether the left side value is greater than the right side value. If yes then returns true otherwise false.
>=Check whether the left operand is less than the right operand. If yes then returns true otherwise false.
<=Check whether the left operand is greater than or equal to the right operand. If yes then returns true otherwise false.

Example: Comparison Operators

var a = 6, b = 12, c = "6";
var x = a;

a == c; // returns true

a === c; // returns false

a == x; // returns true

a != b; // returns true

a > b; // returns false

a < b; // returns true

a >= b; // returns false

a <= b; // returns true

a >= c; // returns true

a <= c; // returns true

Logical Operators

Logical operators are used to combine two or more conditions. JavaScript includes following logical operators.

OperatorDescription
&&&& is known as AND operator. It checks whether two operands are non-zero (0, false, undefined, null or “” are considered as zero), if yes then returns 1 otherwise 0.
|||| is known as OR operator. It checks whether any one of the two operands is non-zero (0, false, undefined, null or “” is considered as zero).
!! is known as NOT operator. It reverses the boolean result of the operand (or condition)

Example: Logical Operators


var a = 5, b = 10;

(a != b) && (a < b); // returns true

(a > b) || (a == b); // returns false

(a < b) || (a == b); // returns true

!(a < b); // returns false

!(a > b); // returns true

Assignment Operators:

JavaScript includes assignment operators to assign values to variables with fewer keystrokes.

Assignment operatorsDescription
=Assigns right operand value to left operand.
+=Sums up left and right operand values and assign the result to the left operand.
-=Subtract the right operand value from the left operand value and assign the result to the left operand.
*=Multiply left and right operand values and assign the result to the left operand.
/=Divide the left operand value by the right operand value and assign the result to the left operand.
%=Subtract the right operand value from the left operand value and assign the result to the left operand.

Example: Assignment operators

var x = 5, y = 10;

x = y; //x would be 10

x += 1; //x would be 11

x -= 1; //x would be 10

x *= 5; //x would be 50

x /= 5; //x would be 10

x %= 2; //x would be 0

Take Your Free Live Demo Class Now

Enter Your DETAILS BELOW

To Get Complete Information or We Will Call You

We Will Send You the Course Fees Details In Below Number

Enter Your DETAILS BELOW

We Will Call Back You Shortly

Enter Your DETAILS BELOW

We Will Call Back You Shortly

Enter Your DETAILS BELOW

Enter Your DETAILS BELOW TO take

free demo class