JavaScript Operators
JavaScript operators are the foundation of performing operations and comparisons in the language. They let you manipulate data, evaluate conditions, and build complex logic. In this post, we’ll cover the four primary types of operators: arithmetic, comparison, logical, and assignment.
Arithmetic Operators
Section titled “Arithmetic Operators”Arithmetic operators are used to perform mathematical calculations.
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 6 | 4 |
* | Multiplication | 4 * 7 | 28 |
/ | Division | 20 / 4 | 5 |
% | Modulus (remainder) | 10 % 3 | 1 |
let a = 20;let b = 4;console.log(a + b); // Outputs: 24console.log(a - b); // Outputs: 16console.log(a * b); // Outputs: 80console.log(a / b); // Outputs: 5console.log(a % b); // Outputs: 0
Comparison Operators
Section titled “Comparison Operators”Comparison operators are used to compare two values and return a boolean (true
or false
) result.
Operator | Description | Example | Result |
---|---|---|---|
== | Equal to (value only) | 5 == '5' | true |
=== | Strict equal to (value + type) | 5 === '5' | false |
!= | Not equal to (value only) | 5 != '6' | true |
< | Less than | 5 < 10 | true |
> | Greater than | 10 > 5 | true |
Examples:
Section titled “Examples:”console.log(x == y); // Outputs: true (loose equality, checks only value)console.log(x === y); // Outputs: false (strict equality, checks value and type)console.log(x != y); // Outputs: false (values are equal)console.log(x < 10); // Outputs: trueconsole.log(x > 1); // Outputs: true
Note: Always prefer using ===
and !==
over ==
and !=
to avoid unexpected type coercion issues.
Logical Operators
Section titled “Logical Operators”Logical operators allow you to combine multiple conditions or evaluate logical expressions.
| Operator | Description | Example | Result |
| -------- | ----------- | --------------- | ---------- | ----- | --- | ------ | ------ |
| &&
| Logical AND | true && false
| false
|
| | |
| Logical OR | true | | false
| true
|
| !
| Logical NOT | !true
| false
|
Logical AND (&&
)
Section titled “Logical AND (&&)”Returns true
only if both conditions are true.
let a = true;let b = false;console.log(a && b); // Outputs: falseconsole.log(a && !b); // Outputs: true
Logical OR (||
)
Section titled “Logical OR (||)”Returns true
if one or more conditions are true.
let age = 18;console.log(age > 16 || age < 12); // Outputs: trueconsole.log(age > 20 || age < 10); // Outputs: false
Logical NOT (!
)
Section titled “Logical NOT (!)”Reverses a boolean value.
let isActive = true;console.log(!isActive); // Outputs: false
Assignment Operators
Section titled “Assignment Operators”Assignment operators are used to assign values to variables. You can perform operations while assigning values.
Operator | Description | Example | Equivalent To |
---|---|---|---|
= | Assign | x = 5 | — |
+= | Add and assign | x += 3 | x = x + 3 |
-= | Subtract and assign | x -= 2 | x = x - 2 |
*= | Multiply and assign | x *= 4 | x = x * 4 |
/= | Divide and assign | x /= 2 | x = x / 2 |
%= | Modulus and assign | x %= 3 | x = x % 3 |
Examples:
Section titled “Examples:”let x = 10;x += 5; // Equivalent to x = x + 5 console.log(x); // Outputs: 15x *= 2; // Equivalent to x = x * 2 console.log(x); // Outputs: 30x -= 10; // Equivalent to x = x - 10 console.log(x); // Outputs: 20x /= 5; // Equivalent to x = x / 5 console.log(x); // Outputs: 4x %= 3; // Equivalent to x = x % 3 console.log(x); // Outputs: 1