JavaScript Operators: Performing Calculations and Comparisons
Operators in JavaScript are symbols used to perform operations on values and variables. They are essential for calculations, comparisons, and decision-making in your code.
🔹 Arithmetic Operators
Used for basic math operations:
- + (Addition) → 5 + 3 = 8
- - (Subtraction) → 10 - 4 = 6
- * (Multiplication) → 2 * 3 = 6
- / (Division) → 12 / 4 = 3
- % (Modulus) → 10 % 3 = 1 (remainder)
- ** (Exponentiation) → 2 ** 3 = 8
🔹 Assignment Operators
Used to assign values to variables:
- = → Assigns a value (let x = 5;)
- += → Adds and assigns (x += 2;)
- -= → Subtracts and assigns (x -= 1;)
- *= → Multiplies and assigns (x *= 3;)
🔹 Comparison Operators
Used to compare values:
- == → Equal to (checks value only)
- === → Strict equal (checks value and type)
- != → Not equal
- !== → Strict not equal
- > → Greater than
- < → Less than
- >= → Greater than or equal to
- <= → Less than or equal to
🔹 Logical Operators
Used to combine conditions:
- && (AND) → True if both conditions are true
- || (OR) → True if at least one condition is true
- ! (NOT) → Reverses the condition
Example:
🔹 Best Practices
- Use === instead of == to avoid type conversion issues.
- Keep expressions simple for readability.
- Combine logical operators carefully to avoid confusion.
By mastering JavaScript operators, you’ll be able to perform calculations, make decisions, and control the flow of your programs effectively.
Comments (Write a comment)
Showing comments related to this blog.