Description
JavaScript Operators are the symbols and keywords that tell JavaScript to perform some action, based on the kind of operator used.
All the operators are categorized into the below categories.
- Arithmetic Operators
- Assignment Operators
- String Operators
- Incrementing and Decrementing Operators
- Logical Operators
- Comparison Operators
Arithmetic Operators
The arithmetic operators are used to perform arithmetic operations.
Operator | Name | Example | Result |
+ | Addition | x + y | Returns the sum of x and y. |
- | Subtraction | x - y | Returns the difference of x and y. |
* | Multiplication | x * y | Returns the product of x and y. |
/ | Division | x / y | Returns the quotient of x and y. |
% | Modulus | x % y | Returns the remainder of x divided by y. |
The below example has used arithmetic operators.
var x = 10;
var y = 4;
document.write(x + y); // Prints: 14
document.write(x - y); // Prints: 6
document.write(x * y); // Prints: 40
document.write(x / y); // Prints: 2.5
document.write(x % y); // Prints: 2
Assignment Operators
The assignment operators are used to assign values to variables.
Operator | Name | Example | Result |
= | Assign | x = y | Assigns y to x. |
+= | Add and assign | x += y | Same as (x = x + y). So, it assigns x+y to x. |
-= | Subtract and assign | x -= y | Same as (x = x - y). So, it assigns x-y to x. |
*= | Multiply and assign | x *= y | Same as (x = x * y). So, it assigns x*y to x. |
/= | Divide and assign quotient | x /= y | Same as (x = x / y). so, it assigns x/y to x. |
%= | Divide and assign modulus | x %= y | Same as (x = x % y). So, it assigns x%y to x. |
The below example has used assignment operators.
var x;
x = 10;
document.write(x); // Prints: 10
x = 20;
x += 30;
document.write(x); // Prints: 50
x = 50;
x -= 20;
document.write(x); // Prints: 30
x = 5;
x *= 25;
document.write(x); // Prints: 125
x = 50;
x /= 10;
document.write(x); // Prints: 5
x = 100;
x %= 15;
document.write(x); // Prints: 10
Incrementing and Decrementing Operators
The incrementing/decrementing operators are used to increment/decrement a value compare two values and return TRUE or FALSE.
Operator | Name | Example | Result |
++x | Pre-increment | y = ++x | Increments x by one, and then returns x. So, y contains x+1. |
x++ | Post-increment | y = x++ | Returns x, and then increments x by one. So, y contains x. |
--x | Pre-decrement | y = --x | Decrements x by one, and then returns x. So, y contains x-1. |
x-- | Post-decrement | y = x-- | Returns x, and then decrements x by one. So, y contains x. |
The below example has used incrementing and decrementing operators.
var x;
x = 10;
document.write(++x); // Prints: 11
document.write(x); // Prints: 11
x = 10;
document.write(x++); // Prints: 10
document.write(x); // Prints: 11
x = 10;
document.write(--x); // Prints: 9
document.write(x); // Prints: 9
x = 10;
document.write(x--); // Prints: 10
document.write(x); // Prints: 9
String Operators
The string operators are used to perform operations on string values and return a resulted string.
Operator | Name | Example | Result |
+ | Concatenation | str1 + str2 | Returns the concatenation string of str1 and str2. |
+= | Append | str1 += str2 | Appends the str2 to the str1. |
The below example has used string operators.
var str1 = "Hello";
var str2 = " World!";
document.write(str1 + str2); // Prints: Hello World!
str1 += str2;
document.write(str1); // Prints: Hello World!
Logical Operators
The logical operators are used to combine two conditional statements and return TRUE or FALSE.
Operator | Name | Example | Result |
&& | AND | x && y | Returns TRUE if both x and y are TRUE. |
|| | OR | x || y | Returns TRUE if either x or y is TRUE. |
! | NOT | ! x | Returns TRUE if x is FALSE. |
The below example has used logical operators.
var year = 2020;
// Leap years are divisible by 400 (or) divisible by 4 but not 100
if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))){
document.write("Year " + year + " is a leap year.");
} else{
document.write("Year " + year + " is not a leap year.");
}
Comparison Operators
The comparison operators are used to compare two values and return TRUE or FALSE.
Operator | Name | Example | Result |
== | Equal | x == y | Returns TRUE if x is equal to y. |
=== | Identical (or Strict Equal) | x === y | Returns TRUE if x is equal to y, and they are having same data type. |
!= | Not Equal | x != y | Returns TRUE if x is not equal to y. |
!== | Not Identical | x !== y | Returns TRUE if x is not equal to y, or they are having different data types. |
< | Less than | x < y | Returns TRUE if x is less than y. |
> | Greater than | x > y | Returns TRUE if x is greater than y. |
<= | Less than or equal to | x <= y | Returns TRUE if x is less than or equal to y. |
>= | Greater than or equal to | x >= y | Returns TRUE if x is greater than or equal to y. |
The below example has used comparison operators.
var x = 10;
var y = 15;
var z = "10";
document.write(x == z); // Prints: true
document.write(x === z); // Prints: false
document.write(x != y); // Prints: true
document.write(x !== z); // Prints: true
document.write(x < y); // Prints: true
document.write(x > y); // Prints: false
document.write(x <= y); // Prints: true
document.write(x >= y); // Prints: false
Overall
JavaScript Operators are useful in performing some actions and returning the result.