In JavaScript, operators are used to perform operations on variables and values. There are several types of operators in JavaScript, including:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Conditional (ternary) operator
Let’s take a closer look at each type of operator.
- Arithmetic operators:
Operator | Description | Example |
---|---|---|
+ | Addition | 5 + 2 |
– | Subtraction | 5 – 2 |
* | Multiplication | 5 * 2 |
/ | Division | 5 / 2 |
% | Modulus (remainder) | 5 % 2 |
Example program:
let x = 5;
let y = 2;
console.log(x + y); // Output: 7
console.log(x - y); // Output: 3
console.log(x * y); // Output: 10
console.log(x / y); // Output: 2.5
console.log(x % y); // Output: 1
2. Assignment operators:
Operator | Description | Example |
---|---|---|
= | Assign value | x = 5 |
+= | Add and assign | x += 2 |
-= | Subtract and assign | x -= 2 |
*= | Multiply and assign | x *= 2 |
/= | Divide and assign | x /= 2 |
%= | Modulus and assign | x %= 2 |
Example program:
let x = 5;
x += 2;
console.log(x); // Output: 7
x -= 2;
console.log(x); // Output: 5
x *= 2;
console.log(x); // Output: 10
x /= 2;
console.log(x); // Output: 5
x %= 2;
console.log(x); // Output: 1
3. Comparison operators:
Operator | Description | Example |
---|---|---|
== | Equal to (loose equality) | 5 == “5” |
=== | Equal to (strict equality) | 5 === 5 |
!= | Not equal to (loose inequality) | 5 != “5” |
!== | Not equal to (strict inequality) | 5 !== “5” |
> | Greater than | 5 > 2 |
< | Less than | 5 < 2 |
>= | Greater than or equal to | 5 >= 5 |
<= | Less than or equal to | 5 <= 2 |
Example program:
console.log(5 == "5"); // Output: true
console.log(5 === "5"); // Output: false
console.log(5 != "5"); // Output: false
console.log(5 !== "5"); // Output: true
console.log(5 > 2); // Output: true
console.log(5 < 2); // Output: false
console.log(5 >= 5); // Output: true
console.log(5 <= 2); // Output: false
4. Logical operators:
Operator | Description | Example |
---|---|---|
&& | Logical and | true && false |
|| | Logical or | true || false |
! | Logical not | !(true && false) |
Example program:
let x = 5;
let y = 2;
console.log((x > 2) && (y < 1)); // Output: false
console.log((x > 2) || (y < 1)); // Output: true
console.log(!(x > 2)); // Output: false
console.log(!(y < 1)); // Output: true
5. Conditional (ternary) operator:
The ternary operator is a shorthand way of writing an if-else statement in a single line.
Example program:
let x = 5;
console.log(x > 2 ? "x is greater than 2" : "x is less than or equal to 2"); // Output: "x is greater than 2"
This is equivalent to writing:
if (x > 2) {
console.log("x is greater than 2");
} else {
console.log("x is less than or equal to 2");
}
Operators are used extensively in JavaScript to perform various tasks such as arithmetic calculations, logical evaluations, and assignment operations. By understanding the different types of operators and how they work, you can write more efficient and effective JavaScript code.