All tutorials
Basics

Operators

Arithmetic, comparison, logical, assignment and ternary operators.

1. Explanation

Operators combine or compare values. Arithmetic: + - * / % **. Comparison: === !== > < >= <=. Always prefer === (strict equality) over == (loose). Logical: && || !. Assignment: = += -= *=. Ternary: condition ? a : b. Nullish coalescing ?? returns the right side only when the left is null or undefined.

2. Example

Try it yourself
Console
Click Run to see output.

3. Expected output

output
15
1
256
false
true
yes
fallback
Adult

4. Common mistakes

  • Using == instead of === leads to surprising coercions
  • Confusing % (remainder) with division
  • Forgetting operator precedence: 2 + 3 * 4 is 14, not 20

5. Practice questions

  1. Check if a number is divisible by 7
  2. Use a ternary to print 'pass' or 'fail' for a score >= 40
  3. Use ?? to provide a default for an undefined variable
→ Try more practice problems