Ch 5 · Operators & Expressions · JavaScript

Topic 5 of 13 in JavaScript — Complete Tutorial (Beginner) — 5 lessons.

What is an Operator?

An operator is a special symbol that tells JavaScript to perform a specific action on one or more values. You already know the most common operators from school mathematics — the + symbol means add, the - symbol means subtract. In JavaScript, operators cover a much wider range of actions than just arithmetic. There are operators for comparing two values, for combining logical conditions, for assigning values into variables, and for performing several common update operations in shorthand.

Arithmetic Operators

Arithmetic operators perform mathematical calculations on number values. Most of them map directly to the symbols you already use in maths class. The one that may be new to you is the modulus operator %, which gives you the remainder left over after division rather than the quotient. For example, 17 % 5 gives 2, because 17 divided by 5 is 3 with a remainder of 2. The modulus operator turns out to be one of the most frequently used tools in real programs — it is perfect for checking if a number is even or odd, for cycling a counter back to zero after it reaches a maximum, or for distributing items evenly across rows.

Comparison Operators

Comparison operators compare two values and always produce a boolean result — either true or false. There is a very important distinction between == (loose equality) and === (strict equality) that every JavaScript programmer must understand and remember. The loose == operator performs type conversion before comparing — it might decide that the number 5 and the string "5" are equal because it secretly converts one to match the other. The strict === operator insists that both the value AND the type must match — 5 === "5" is false because even though the digits look the same, one is a number and the other is a string. In JavaScript, you should use === in almost every situation. It is safer, clearer in intent, and prevents a whole class of subtle bugs.

Logical Operators

Logical operators let you combine multiple boolean conditions into a single decision. The AND operator && requires every condition to be true for the overall result to be true — think of it as a checklist where every box must be ticked. The OR operator || requires only one condition to be true — think of it as alternative routes where any one open path lets you through. The NOT operator ! simply flips a boolean: true becomes false and false becomes true. You will use these three operators constantly whenever your program needs to make complex decisions based on multiple factors at once.

Assignment Operators

Assignment operators are shorthand ways to update a variable using its current value. Writing score += 10 means exactly the same thing as writing score = score + 10 — it takes whatever value score currently holds, adds 10 to it, and stores the result back in score. These compact operators are used constantly whenever a value changes incrementally: counting up a score, reducing a countdown, or doubling an amount. They save you from writing the variable name twice every time you update it, which reduces typing and makes the code easier to read.

All topics in JavaScript Beginner

  1. Introduction to Programming & JavaScript
  2. Your First JavaScript Program
  3. Variables — Your Data Containers
  4. Data Types — Kinds of Values
  5. Operators & Expressions
  6. Conditional Statements
  7. Loops & Iterations
  8. Functions
  9. Arrays — Lists of Data
  10. Objects & Classes
  11. Strings — Working with Text
  12. Error Handling
  13. Mini-Projects & Practice