JS Operators

In this tutorial, you will learn how to manipulate or perform the operations on variables and values using the operators in JavaScript.

 

What are Operators in JavaScript

Operators are known to be symbols or keywords that program the JavaScript engine to perform some sort of action. For instance, the addition (+) symbol is an operator that instructs the JavaScript engine to add two variables or values together, while the equal to (=), greater than (>), or less than (<) symbols are the operators that instruct JavaScript engine to make a comparison between two variables or values, and so on.

The following sections describe the different operators used in JavaScript.
 

JavaScript Arithmetic Operators

Common arithmetical operations such as addition, subtraction, multiplication of values, and variables are performed by the JavaScript arithmetic operators. Here is a complete list of JavaScript's arithmetic operators:

OperatorDescriptionExampleResult
+Additionx + ySum of x and y
-Subtractionx - yDifference of x and y.
*Multiplicationx * yProduct of x and y.
/Divisionx / yQuotient of x and y
%Modulusx % yRemainder of x divided by y

 

The following example will show you these arithmetic operators in action:

var x = 10;
var y = 4;
alert(x + y); // 0utputs: 14
alert(x - y); // 0utputs: 6
alert(x * y); // 0utputs: 40
alert(x / y); // 0utputs: 2.5
alert(x % y); // 0utputs: 2

Try With Example

 

JavaScript Assignment Operators

The assignment operators are used to assign values to variables.

OperatorDescriptionExampleIs The Same As
=Assignx = yx = y
+=Add and assignx += yx = x + y
-=Subtract and assignx -= yx = x - y
*=Multiply and assignx *= yx = x * y
/=Divide and assign quotientx /= yx = x / y
%=Divide and assign modulusx %= yx = x % y

The following example will show you these assignment operators in action:

Example:

var x;    // Declaring Variable
 
x = 10;
alert(x); // Outputs: 10
 
x = 20;
x += 30;
alert(x); // Outputs: 50
 
x = 50;
x -= 20;
alert(x); // Outputs: 30
 
x = 5;
x *= 25;
alert(x); // Outputs: 125
 
x = 50;
x /= 10;
alert(x); // Outputs: 5
 
x = 100;
x %= 15;
alert(x); // Outputs: 10

Try With Example

 

JavaScript String Operators

There are two operators which can also be used for strings.

OperatorDescriptionExampleResult
+Concatenationstr1 + str2Concatenation of str1 and str2
+=Concatenation assignmentstr1 += str2Appends the str2 to the str1

The following example will show you these string operators in action:

var str1 = "Hello";
var str2 = " World!";
 
alert(str1 + str2); // Outputs: Hello World!
 
str1 += str2;
alert(str1); // Outputs: Hello World!

Try With Example

 

JavaScript Incrementing and Decrementing Operators

The increment or decrement operators are used to either increment or decrement a variable's value.

OperatorNameEffect
++xPre-incrementIncrements x by one, then returns x
x++Post-incrementReturns x, then increments x by one
--xPre-decrementDecrements x by one, then returns x
x--Post-decrementReturns x, then decrements x by one

The following example will demonstrate how increment and decrement operators work:

var x; // Declaring Variable
 
x = 10;
alert(++x); // Outputs: 11
alert(x);   // Outputs: 11
 
x = 10;
alert(x++); // Outputs: 10
alert(x);   // Outputs: 11
 
x = 10;
alert(--x); // Outputs: 9
alert(x);   // Outputs: 9
 
x = 10;
alert(x--); // Outputs: 10
alert(x);   // Outputs: 9

Try With Example

 

JavaScript Logical Operators

The logical operators are typically used to combine conditional statements.

OperatorNameExampleResult
&&Andx && yTrue if both x and y are true
||Orx || yTrue if either x or y is true
!Not!xTrue if x is not true

The logical operators are typically used to combine conditional statements.

The following example will show you how these logical operators actually work:

Example:

var year = 2018;
 
// Leap years are divisible by 400 or by 4 but not 100
if((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0))){
    alert(year + " is a leap year.");
} else{
    alert(year + " is not a leap year.");
}

Try With Example

You will learn about conditional statements in JavaScript if/else chapter.

 

JavaScript Comparison Operators

The comparison operators are used to compare two values in a Boolean style.

OperatorNameExampleResult
==Equalx == yTrue if x is equal to y
===Identicalx === yTrue if x is equal to y, and they are of the same type 
!=Not equalx != yTrue if x is not equal to y
!==Not identicalx !== yTrue if x is not equal to y, or they are not of the same type
<Less thanx < yTrue if x is less than y
>Greater thanx > yTrue if x is greater than y
>=Greater than or equal tox >= yTrue if x is greater than or equal to y
<=Less than or equal tox <= yTrue if x is less than or equal to y

The following example will show you these comparison operators in action:

Example:

var x = 25;
var y = 35;
var z = "25";
 
alert(x == z);  // Outputs: true
alert(x === z); // Outputs: false
alert(x != y);  // Outputs: true
alert(x !== z); // Outputs: true
alert(x < y);   // Outputs: true
alert(x > y);   // Outputs: false
alert(x <= y);  // Outputs: true
alert(x >= y);  // Outputs: false

Try With Example