JS Operators
JavaScript operators are used to perform operations on variables and values. They are categorized into different types based on their functionality.
1. Arithmetic Operators
Used for mathematical calculations.
Operator | Description | Example | Output |
---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 6 * 2 | 12 |
/ | Division | 10 / 2 | 5 |
% | Modulus (Remainder) | 10 % 3 | 1 |
** | Exponentiation (ES6) | 2 ** 3 | 8 |
++ | Increment | let x = 5; x++ | 6 |
-- | Decrement | let y = 5; y-- | 4 |
Example:
let a = 10, b = 3;
console.log(a + b); // Output: 13
console.log(a % b); // Output: 1
console.log(a ** b); // Output: 1000
2. Assignment Operators
Used to assign values to variables.
Operator | Description | Example | Equivalent To |
= | Assign | x = 5 | x = 5 |
+= | Add & Assign | x += 3 | x = x + 3 |
-= | Subtract & Assign | x -= 2 | x = x - 2 |
*= | Multiply & Assign | x *= 4 | x = x * 4 |
/= | Divide & Assign | x /= 2 | x = x / 2 |
%= | Modulus & Assign | x %= 3 | x = x % 3 |
**= | Exponent & Assign | x **= 2 | x = x ** 2 |
3. Comparison Operators
Used to compare values and return true or false.