|
| 1 | +// 📌 JavaScript Basics - Operators |
| 2 | + |
| 3 | +// Welcome to the Operators section of the JavaScript Basics tutorial! |
| 4 | +// Here, you'll learn about different types of operators in JavaScript. |
| 5 | + |
| 6 | +// Arithmetic Operators |
| 7 | +// Used for mathematical calculations. |
| 8 | +let a = 10; |
| 9 | +let b = 5; |
| 10 | +console.log(a + b); // Addition ➝ 15 |
| 11 | +console.log(a - b); // Subtraction ➝ 5 |
| 12 | +console.log(a * b); // Multiplication ➝ 50 |
| 13 | +console.log(a / b); // Division ➝ 2 |
| 14 | +console.log(a % b); // Modulus (Remainder) ➝ 0 |
| 15 | +console.log(a ** b); // Exponentiation ➝ 10^5 = 100000 |
| 16 | + |
| 17 | +// Assignment Operators |
| 18 | +// Used to assign values to variables. |
| 19 | +let x = 10; |
| 20 | +x += 5; // x = x + 5 ➝ 15 |
| 21 | +x -= 3; // x = x - 3 ➝ 12 |
| 22 | +x *= 2; // x = x * 2 ➝ 24 |
| 23 | +x /= 4; // x = x / 4 ➝ 6 |
| 24 | +x %= 2; // x = x % 2 ➝ 0 |
| 25 | +x **= 3; // x = x ** 3 ➝ 0 |
| 26 | + |
| 27 | +// Comparison Operators |
| 28 | +// Used to compare values. |
| 29 | +console.log(10 == "10"); // Equal (checks value) ➝ true |
| 30 | +console.log(10 === "10"); // Strict Equal (checks value & type) ➝ false |
| 31 | +console.log(10 != 5); // Not Equal ➝ true |
| 32 | +console.log(10 !== "10"); // Strict Not Equal ➝ true |
| 33 | +console.log(10 > 5); // Greater than ➝ true |
| 34 | +console.log(10 < 20); // Less than ➝ true |
| 35 | +console.log(10 >= 10); // Greater than or equal ➝ true |
| 36 | +console.log(5 <= 10); // Less than or equal ➝ true |
| 37 | + |
| 38 | +// Logical Operators |
| 39 | +// Used for logical expressions. |
| 40 | +let isTrue = true; |
| 41 | +let isFalse = false; |
| 42 | +console.log(isTrue && isFalse); // AND ➝ false |
| 43 | +console.log(isTrue || isFalse); // OR ➝ true |
| 44 | +console.log(!isTrue); // NOT ➝ false |
| 45 | + |
| 46 | +// Bitwise Operators |
| 47 | +// Used for bitwise operations (advanced usage). |
| 48 | +console.log(5 & 1); // AND ➝ 1 (0101 & 0001 = 0001) |
| 49 | +console.log(5 | 1); // OR ➝ 5 (0101 | 0001 = 0101) |
| 50 | +console.log(5 ^ 1); // XOR ➝ 4 (0101 ^ 0001 = 0100) |
| 51 | +console.log(~5); // NOT ➝ -6 (~00000101 = 11111010) |
| 52 | +console.log(5 << 1); // Left shift ➝ 10 |
| 53 | +console.log(5 >> 1); // Right shift ➝ 2 |
| 54 | + |
| 55 | +// Ternary Operator |
| 56 | +// A shorthand for `if...else` statements. |
| 57 | +let age = 20; |
| 58 | +let status = age >= 18 ? "Adult" : "Minor"; |
| 59 | +console.log(status); // ➝ "Adult" |
| 60 | + |
| 61 | +// 💡 Experiment with these operators to understand their behavior! |
0 commit comments