Operators
Operators let you calculate and compare values. Arithmetic operators handle math: + (add), - (subtract), * (multiply), / (divide), % (remainder, called "modulo" 7 % 2 evaluates to 1). Comparison operators check relationships between values and return a Boolean: > < >= <= work as expected. Equality is trickier: == checks value only and allows type conversion ("5" == 5 is true), while === checks both value and type without conversion ("5" === 5 is false). Modern JavaScript almost always prefers === for this reason it avoids surprising conversions. Logical operators combine conditions: && (AND, both sides must be true), || (OR, at least one side must be true), ! (NOT, reverses a Boolean). Assignment operators go beyond plain =: += adds and reassigns in one step (score += 10; is the same as score = score + 10;), and -=, *=, /= work the same way for their operations.
Why this matters
- ·=== avoids the surprising type conversions that == can silently perform.
- ·Shorthand assignment operators like += make updating a variable's value concise and readable.
- ·Logical operators let a single condition express genuinely complex logic.
Common mistakes
- ·Using == instead of === can produce true for comparisons you'd expect to be false, like "5" == 5.
- ·Confusing = (assignment) with == or === (comparison) is a very common and easy-to-make mistake.
- ·Forgetting operator precedence (like * running before +) can produce a different result than intended.
Frequently asked questions
== compares values and allows type conversion; === compares both value and type, with no conversion. === is generally the safer, more predictable choice.
Practice questions
Question 1
Compare the numbers 10 and 20 with both == and === and display both results.
Question 2
Use the % operator to check whether 15 is evenly divisible by 3, and display the remainder.
Quick check
Just for this topic answer honestly, then check yourself.
1 / 3
