2 2 Operators Explained
Key Concepts
Operators in JavaScript are symbols or keywords that perform operations on operands (values or variables). Understanding operators is crucial for performing arithmetic, comparison, logical, and assignment operations.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division, and more.
- Addition (+): Adds two operands.
- Subtraction (-): Subtracts the second operand from the first.
- Multiplication (*): Multiplies two operands.
- Division (/): Divides the first operand by the second.
- Modulus (%): Returns the remainder of the division of the first operand by the second.
- Increment (++): Increases the value of an operand by 1.
- Decrement (--): Decreases the value of an operand by 1.
Example:
let a = 10; let b = 3; console.log(a + b); // Output: 13 console.log(a - b); // Output: 7 console.log(a * b); // Output: 30 console.log(a / b); // Output: 3.3333333333333335 console.log(a % b); // Output: 1 let c = 5; c++; console.log(c); // Output: 6 let d = 8; d--; console.log(d); // Output: 7
Comparison Operators
Comparison operators are used to compare two values and return a boolean result (true or false).
- Equal (==): Checks if two operands are equal.
- Not Equal (!=): Checks if two operands are not equal.
- Strict Equal (===): Checks if two operands are equal and of the same type.
- Strict Not Equal (!==): Checks if two operands are not equal or not of the same type.
- Greater Than (>): Checks if the first operand is greater than the second.
- Less Than (<): Checks if the first operand is less than the second.
- Greater Than or Equal (>=): Checks if the first operand is greater than or equal to the second.
- Less Than or Equal (<=): Checks if the first operand is less than or equal to the second.
Example:
let x = 5; let y = 10; console.log(x == y); // Output: false console.log(x != y); // Output: true console.log(x === "5"); // Output: false console.log(x !== "5"); // Output: true console.log(x > y); // Output: false console.log(x < y); // Output: true console.log(x >= y); // Output: false console.log(x <= y); // Output: true
Logical Operators
Logical operators are used to combine multiple conditions and return a boolean result.
- Logical AND (&&): Returns true if both operands are true.
- Logical OR (||): Returns true if at least one operand is true.
- Logical NOT (!): Returns the opposite boolean value of the operand.
Example:
let p = true; let q = false; console.log(p && q); // Output: false console.log(p || q); // Output: true console.log(!p); // Output: false console.log(!q); // Output: true
Assignment Operators
Assignment operators are used to assign values to variables.
- Assignment (=): Assigns the value of the right operand to the left operand.
- Addition Assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.
- Subtraction Assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
- Multiplication Assignment (*=): Multiplies the left operand by the right operand and assigns the result to the left operand.
- Division Assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand.
- Modulus Assignment (%=): Assigns the remainder of the division of the left operand by the right operand to the left operand.
Example:
let m = 10; m += 5; // Equivalent to m = m + 5 console.log(m); // Output: 15 let n = 20; n -= 3; // Equivalent to n = n - 3 console.log(n); // Output: 17 let o = 4; o *= 2; // Equivalent to o = o * 2 console.log(o); // Output: 8 let p = 15; p /= 3; // Equivalent to p = p / 3 console.log(p); // Output: 5 let q = 10; q %= 3; // Equivalent to q = q % 3 console.log(q); // Output: 1
Conclusion
Operators are fundamental in JavaScript for performing various operations. Understanding arithmetic, comparison, logical, and assignment operators enables you to write more complex and efficient code.