JavaScript Operators
An operator is a special character in JavaScript that executes operations on operands.
There are many types of operators in JavaScript, such as:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
Arithmetic Operators
Arithmetic Operators are used to execute mathematical operations.
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
++ | Increment |
-- | Decrement |
Example:
let x
=
6;
let y
=
4;
console.log('x + y = ', x + y);
console.log('x - y = ', x - y);
console.log('x * y = ', x * y);
console.log('x / y = ', x / y);
Output:
10
2
24
1.5
Assignment Operators
Assignment operators are utilized to assign values to variables.
Operator | Description | Example |
---|---|---|
= | Assignment operator | a = 5; |
+= | Addition assignment | a += 5; |
-= | Subtraction Assignment | a -= 3; |
*= | Multiplication Assignment | a *= 2; |
/= | Division Assignment | a /= 2; |
%= | Modulus Assignment | a %= 2; |
Example:
let x
=
6;
Comparison Operators
Comparison operators are utilized to compare two values.
Operator | Description |
---|---|
== | Equal |
!= | Not Equal |
> | Greater than |
>= | Greater than or equal to |
<< /td> | Less than |
<=< /td> | Less than or equal to |
Example:
let x
=
5;
let y
=
5;
console.log(x == y);
console.log(x != y);
console.log(x > y);
console.log(x >= y);
console.log(x < y);
console.log(x <= y);
Output:
true
false
false
true
false
true
Logical Operators
Logical operators execute logical operations and give a Boolean value.
Operator | Description | Example |
---|---|---|
&& | Logical AND | x && y |
|| | Logical OR | x || y |
! | Logical NOT | !x |
Example:
let x
=
5;
let y
=
5;
console.log(x && y);
console.log(x || y);
Output:
5
5
Bitwise Operators
Bitwise operators are utilized to work with binary operations.
Operator | Description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
~ | Bitwise NOT |
<<< /td> | Left shift |
Example:
let x
=
10;
let y
=
12;
result = x & y;
console.log(result);
Output:
8