TetSharing
JavaScript tutorial · 19 chapters
  1. Get started
  2. let, const and var
  3. Data types
  4. Operators and equality
  5. Strings
  6. Template literals
  7. if, else and switch
  8. Loops
  9. Functions
  10. Arrow functions
  11. Arrays
  12. map, filter, reduce
  13. Objects
  14. Destructuring and spread
  15. Classes
  16. Errors and try...catch
  17. Promises
  18. async and await
  19. JSON

Final JavaScript quiz
Cheat sheet: javascript array methods

Chapter 4 of 19 · By Ethan Cole · Updated November 21, 2026

JavaScript Operators: Arithmetic, === and Logical

Operators combine values into new values. Most look familiar from maths, but JavaScript's automatic type conversion gives a few of them behavior you need to know about.

Arithmetic and the two faces of +

+ - * / work as expected, % gives the remainder, and ** raises to a power. Division always produces a decimal when needed: 7 / 2 is 3.5.

+ has a second job: if either side is a string, it concatenates. So "5" + 3 is the string "53". The other arithmetic operators only do maths, so "5" - 3 converts the string and gives 2. When adding values that might be strings, convert them with Number() first.

Strict and loose equality

=== (strict equality) is true only when both values have the same type and value. == (loose equality) first converts the values to a common type, which leads to results like 1 == "1" being true. Always use === and !==. The one widely accepted use of == is x == null, which matches both null and undefined.

NaN is not equal to anything, including itself, so test for it with Number.isNaN().

JavaScript · editable

Expected output

1 1024 3.5
53 2 10
true false
true false
false true
yes default fallback 0
12

Logical operators return values

&& and || do not just return true or false; they return one of their operands, and they short-circuit:

  • a && b returns a if it is falsy, otherwise b.
  • a || b returns a if it is truthy, otherwise b. That makes it a common way to supply defaults.
  • a ?? b (nullish coalescing) returns b only when a is null or undefined.

The difference between || and ?? matters for legitimate falsy values. In the example, 0 || "default" replaces the zero, while 0 ?? "kept" keeps it. For a quantity or a price, ?? is usually what you want.

The falsy values are false, 0, -0, 0n, "", null, undefined and NaN. Everything else, including "0", [] and {}, is truthy.

Assignment shortcuts

count++ adds one, count-- subtracts one, and compound operators like +=, *= and ??= update a variable in place.

Common mistakes

  • Using == and getting surprised by conversions such as "" == 0 being true.
  • Using || for defaults when 0 or "" are valid values.
  • Comparing with NaN using ===.

Summary

  • + concatenates when either side is a string; other operators convert to numbers.
  • Use === and !==; loose == converts types.
  • &&, || and ?? return operands and short-circuit; ?? only replaces null and undefined.

Quiz: Operators and equality

1. What is "5" + 3?

With a string on one side, + joins the values as text.

2. What does 1 === "1" return?

Strict equality requires the same type, and these are a number and a string.

3. What is 0 ?? 10?

?? only falls back when the left side is null or undefined, and 0 is neither.

4. Which is the correct way to check whether x is NaN?

NaN is never equal to itself, so use Number.isNaN().

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.