Operators are used to perform different types of operation on data (variable data or data itself).
JavaScript provide different types of JavaScript operators and they are as follows.
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- String Operators
- Logical Operators
- Bitwise Operators
- Ternary Operators
- Type Operators
Arithmetic Operators
To perform mathematical operation on numbers or variable holding numbers.
+ | Addition (Add two or more values) |
– | Subtraction (Subtract two or more values) |
* | Multiplication (Multiply two or more values) |
/ | Division (Divides a value with another value) |
% | Remainder (Returns a remainder after division) |
** | Exponentiation (Raise First number power of second number ) |
++ | Increment (Increase value by 1) |
– – | Decrement (Decrease value by 1) |
Example
<script>
let num1 = 60;
let num2 = 40;
//Addition
let result = num1 + num2;
document.write(result);
//Subtraction
result = num1 - num2;
document.write(result);
//Multiplication
result = num1 * num2;
document.write(result);
//Division
result = num1 / num2;
document.write(result);
//Remainder
result = num1 % num2;
document.write(result);
//Exponentiation
result = num1 ** num2;
document.write(result);
//Increment
result = num1++;
document.write(result);
//Decrement
result = num2--;
document.write(result);
</script>
Assignment Operators
= (equals to) is assignment operator and assigns value to a variable.
<script>
let num1 = 60;
let num2 = 40;
num1 = 30;
</script>
num1 is a variable and value 60 is assigned using = (assignment) operator, similarly num2 is assigned with value 40.
String Operators
Concatenation operator or + symbol – It appends or adds a new string after another string.
<script>
let str1 = "Hello ";
let str2 = "World";
let r = str1 + str2;
</script>
Comparison Operators
Compare values or types (sometime both) and return true or false.
Operator | Use |
== | equals to |
=== | both value and type are equal |
!= | not equal |
!== | both value and type are not equal |
> | greater than |
< | less than |
>= | greater than or equals to |
<= | less than or equals to |
? | ternary operator |
Example
Operators | let a = 5, b = 20; | Returns |
== | a == b | false |
=== | a === b | true |
!= | a != b | true |
!== | a !== b | true |
> | a > b | false |
< | a < b | true |
>= | a >= b | false |
<= | a <= b | true |
Logical Operators
Operators | Use |
&& | logical AND |
|| | logical OR |
! | logical NOT |
Example
Operators | let a = 20, b = 50, c =30; | Returns |
&& | (a ==b && a > c) | false |
|| | (a !=b || a > c) | true |
! | !(a) | true |
Ternary Operator
It is also a conditional operator with variable on left side and conditions on right side.
Syntax
variable_name = (condition) ? value1:value2
Example
<script>
let number = 20;
let result = (number % 2) ? "odd number":"even number";
</script>
Bitwise Operators
Operators | Name |
& | AND |
| | OR |
~ | NOT |
^ | XOR |
<< | left shift |
>> | right shift |
>>> | unsigned right shift |
Type Operators
Operator | Description |
typeof | Returns variable type |
instanceof | Returns true, if object is type of class or returns false if not object is of type class. |