Operators in JavaScript are symbols that perform operations on values and variables. They allow you to manipulate data, compare values, and make decisions in your code.
For example, you can use operators to add numbers together, compare two values, or check conditions in an if statement:
let total = 10 + 5; // Addition operator
console.log(total); // Output: 15
console.log(10 > 5); // Greater than operator, Output: true
console.log(true && false); // Logical AND operator, Output: false
Operators are a fundamental part of JavaScript and are used in almost every program. Let’s explore the different types of operators and how they work.
+ (Addition): Adds two numbers (e.g., 5 + 3 = 8).
– (Subtraction): Subtracts the right number from the left (e.g., 10 – 4 = 6).
* (Multiplication): Multiplies two numbers (e.g., 3 * 4 = 12).
/ (Division): Divides the left number by the right (e.g., 10 / 2 = 5).
% (Modulus): Returns the remainder of a division (e.g., 10 % 3 = 1).
** (Exponentiation): Raises a number to the power of another (e.g., 2 ** 3 = 8).
= (Assignment): Assigns a value to a variable (e.g., x = 10).
+= (Addition Assignment): Adds a value and assigns the result (e.g., x += 5 is x = x + 5).
-= (Subtraction Assignment): Subtracts a value and assigns the result (e.g., x -= 2 is x = x – 2).
*= (Multiplication Assignment): Multiplies and assigns the result (e.g., x *= 3 is x = x * 3).
/= (Division Assignment): Divides and assigns the result (e.g., x /= 2 is x = x / 2).
%= (Modulus Assignment): Assigns the remainder (e.g., x %= 3 is x = x % 3).
**= (Exponentiation Assignment): Assigns the result of exponentiation (e.g., x **= 2 is x = x ** 2).
== (Equal To): Checks if values are equal (e.g., 5 == “5” is true).
=== (Strict Equal): Checks if values and types are equal (e.g., 5 === “5” is false).
!= (Not Equal): Checks if values are not equal (e.g., 10 != 5 is true).
!== (Strict Not Equal): Checks if values and types are not equal (e.g., 10 !== “10” is true).
> (Greater Than): Checks if the left value is greater (e.g., 8 > 5 is true).
< (Less Than): Checks if the left value is smaller (e.g., 3 < 7 is true). >= (Greater Than or Equal To): Checks if the left value is greater or equal (e.g., 6 >= 6 is true).
<= (Less Than or Equal To): Checks if the left value is smaller or equal (e.g., 4 <= 5 is true).
&& (AND): Returns true if both conditions are true (e.g., true && false is false).
|| (OR): Returns true if at least one condition is true (e.g., true || false is true).
! (NOT): Reverses a boolean value (e.g., !true is false).
& (Bitwise AND): Performs AND on binary values (e.g., 5 & 1 = 1).
| (Bitwise OR): Performs OR on binary values (e.g., 5 | 1 = 5).
^ (Bitwise XOR): Performs XOR on binary values (e.g., 5 ^ 1 = 4).
~ (Bitwise NOT): Inverts bits (e.g., ~5 = -6).
<< (Left Shift): Shifts bits to the left (e.g., 5 << 1 = 10). >> (Right Shift): Shifts bits to the right (e.g., 5 >> 1 = 2).
++ (Increment): Increases a value by 1 (e.g., let x = 5; x++; now x = 6).
— (Decrement): Decreases a value by 1 (e.g., let y = 5; y–; now y = 4).
condition ? expr1 : expr2
Shortens if-else statements into one line. For example:
let result = (5 > 3) ? "Yes" : "No"; // "Yes"
Here’s an example using multiple operators:
let a = 10, b = 5;
let sum = a + b; // 15
let isGreater = a > b; // true
let isEven = (sum % 2 === 0) ? "Even" : "Odd"; // "Odd"
console.log(sum, isGreater, isEven);
Ready to start mastering JavaScript operators? Explore more tutorials on HowCodingWorks and level up your coding skills!