Operators

JavaScript · Lesson 8 of 61 6 min read Updated

An operator is a symbol that does something with values: + adds, > compares, && combines two conditions. The values an operator works on are called operands. In 7 + 3, the operator is + and the operands are 7 and 3.

In this lesson you will learn the operators you will use in almost every program: arithmetic, assignment, comparison and logical operators, plus the rules that decide which one runs first.

Arithmetic operators

These work like the maths you already know, with two extras: % for the remainder and ** for powers.

JavaScriptRun it yourself
const sweets = 17;
const children = 5;

console.log("Add:", sweets + children);
console.log("Subtract:", sweets - children);
console.log("Multiply:", sweets * 2);
console.log("Divide:", sweets / children);
console.log("Remainder:", sweets % children);
console.log("Power:", 2 ** 10);
Output
Add: 22
Subtract: 12
Multiply: 34
Divide: 3.4
Remainder: 2
Power: 1024

The remainder operator % gives what is left over after dividing: 17 sweets shared among 5 children gives each child 3, with 2 left over. It is very useful for checks such as "is this number even?" (n % 2 === 0) or "is this every 4th item?".

Division never rounds: 17 / 5 is 3.4, not 3. Dividing by zero does not crash; it gives the special number Infinity.

Order of operations

JavaScript follows the same order you learned in school: powers first, then multiplication and division, then addition and subtraction. Use brackets to change the order or simply to make your intent obvious.

JavaScriptRun it yourself
const ticket = 100;
const fee = 50;

console.log(ticket + fee * 2);
console.log((ticket + fee) * 2);
Output
200
300

When in doubt, add brackets. They cost nothing and save readers from doing the precedence rules in their heads.

Assignment operators

You already know =, which stores a value. The compound assignment operators combine a calculation with storing the result.

OperatorExampleSame as
+=score += 4score = score + 4
-=stock -= 1stock = stock - 1
*=price *= 2price = price * 2
/=total /= 2total = total / 2
%=n %= 7n = n % 7
**=side **= 2side = side ** 2

There are also ++ and --, which add or subtract exactly one. runs++ is the same as runs += 1. They are common in loops. Use them on a line of their own: console.log(runs++) prints the old value and then increases it, which confuses many readers.

JavaScriptRun it yourself
let runs = 0;
runs += 4;   // a four
runs += 6;   // a six
runs++;      // a single
console.log("Runs:", runs);
Output
Runs: 11

JavaScript also has the logical assignment operators ||=, &&= and ??=. They are covered in Nullish Coalescing & Optional Chaining.

Comparison operators

Comparison operators compare two values and always give a boolean: true or false.

OperatorMeaning
===Equal (same value and same type)
!==Not equal
> and <Greater than, less than
>= and <=Greater than or equal, less than or equal

Always use === and !==

JavaScript has two kinds of equality. Strict equality === checks the value and the type. Loose equality == first converts the values to the same type and then compares, with rules that surprise everyone.

JavaScriptRun it yourself
console.log(5 === "5");
console.log(5 == "5");
console.log(0 == "");
console.log(null == undefined);
console.log("10" < "9");
Output
false
true
true
true
true

5 == "5" is true because == converted the string to a number. 0 == "" is also true, which almost never matches what you mean. Use === and !== every time; when the types differ, convert first with Number() or String().

The last line shows another trap: when both values are strings, < compares them letter by letter, like a dictionary. "1" comes before "9", so "10" < "9" is true. Convert to numbers before comparing numbers you got as text.

Logical operators

Logical operators combine or reverse conditions:

  • && (AND) is true only if both sides are true.
  • || (OR) is true if at least one side is true.
  • ! (NOT) flips true to false and false to true.
JavaScriptRun it yourself
const age = 19;
const hasLicence = true;

console.log(age >= 18 && hasLicence);
console.log(age < 18 || hasLicence);
console.log(!hasLicence);
Output
true
true
false

&& and || are short-circuit operators: they stop as soon as the answer is known. In false && anything, JavaScript never even looks at anything. They also return one of their operands rather than just true or false, so "" || "Guest" gives "Guest". You will use this for default values in Booleans & if...else.

Which operator runs first?

When several operators appear in one expression, precedence decides the order. From first to last:

  1. Brackets ( )
  2. !, typeof, ++, --
  3. **
  4. *, /, %
  5. +, -
  6. <, <=, >, >=
  7. ===, !==
  8. &&
  9. ||
  10. = and the other assignment operators

So price + tax > 500 && isMember adds first, compares next and checks && last, which is usually what you want.

Common mistakes

  • Using = when you mean ===. if (score = 100) stores 100 instead of comparing. Comparisons need ===.
  • Adding numbers that are strings. "40" + "2" is "402". Convert with Number() first.
  • Using ==. Its conversion rules cause hidden bugs. Stick to === and !==.
  • Comparing numbers stored as text. "100" < "25" is true. Compare real numbers.

Key points

  • Arithmetic operators: +, -, *, /, % (remainder) and ** (power).
  • Compound assignment like += updates a variable in one step; ++ adds one.
  • Comparison operators return true or false; always prefer === and !==.
  • &&, || and ! combine conditions and short-circuit.
  • Precedence decides the order; brackets make it explicit.

Exercise

Aarav's monthly bill has a base plan of ₹299 and 3 add-on packs of ₹49 each. Calculate the total. Then check and print whether the total is more than ₹400, whether it is an even number, and whether it is between ₹400 and ₹500 (both conditions together).

Show solution
JavaScript
const basePlan = 299;
const addOns = 3 * 49;
const total = basePlan + addOns;

console.log("Total:", total);
console.log("More than 400:", total > 400);
console.log("Even:", total % 2 === 0);
console.log("Between 400 and 500:", total >= 400 && total <= 500);
Output
Total: 446
More than 400: true
Even: true
Between 400 and 500: true

% with 2 tests for even numbers, and && checks both ends of the range at once.

Quick Quiz

1. What is 20 % 6?

2. What does 7 === "7" give?

3. What is printed by console.log(2 + 3 * 4)?

4. Which expression is true when a user is at least 18 OR has a parent's consent (consent is true), with age being 16?

Learn JavaScript with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call