Tutorials › JavaScript Tutorial › JavaScript Operators

JavaScript Operators

10 min read · Lesson 6 of 23

Operators are the symbols that do work on values: adding, comparing, combining and assigning. You have already used = and +. This lesson covers the operators you will meet most often.

Arithmetic operators

OperatorMeaningExampleResult
+Addition7 + 310
-Subtraction7 - 34
*Multiplication7 * 321
/Division7 / 23.5
%Remainder (modulus)7 % 31
**Exponent (power)2 ** 38
++Increase by 1x++
--Decrease by 1x--
Example JAVASCRIPT
const bundles = 17;
const perCarton = 5;

console.log(Math.floor(bundles / perCarton)); // full cartons
console.log(bundles % perCarton);             // left over
console.log(10 ** 2);
Result
3
2
100
Try it Yourself »

The remainder operator % is surprisingly useful: n % 2 === 0 tells you whether a number is even.

Operator precedence

Just like in school maths, multiplication and division happen before addition and subtraction. Use brackets to control the order.

Example JAVASCRIPT
console.log(2 + 3 * 4);
console.log((2 + 3) * 4);
Result
14
20
Try it Yourself »
Tip: When in doubt, add brackets. They make your intention obvious to the next reader.

Assignment operators

Assignment operators update a variable using its current value.

OperatorSame as
x = 5set x to 5
x += 5x = x + 5
x -= 5x = x - 5
x *= 5x = x * 5
x /= 5x = x / 5
x %= 5x = x % 5
x **= 2x = x ** 2
Example JAVASCRIPT
let wallet = 1000;
wallet -= 300;  // paid rent share
wallet += 150;  // received from a friend
wallet *= 2;    // a lucky week
console.log(wallet);

let visitors = 0;
visitors++;
visitors++;
console.log(visitors);
Result
1700
2
Try it Yourself »

Prefix and postfix increment

++ and -- can go before or after the variable. On a line of their own, both forms do the same thing. The difference only shows when you use the result in the same expression:

  • Postfix x++ gives back the old value, then increases x.
  • Prefix ++x increases x first, then gives back the new value.
Example JAVASCRIPT
let tickets = 5;
const a = tickets++;
console.log(a, tickets);

let seats = 5;
const b = ++seats;
console.log(b, seats);
Result
5 6
6 6
Try it Yourself »
Tip: Mixing ++ into bigger expressions makes code hard to read. Most developers keep count++ on its own line, where the difference does not matter.

Expressions and operands

The values an operator works on are called operands. In price * qty, the operator is * and the operands are price and qty. Any piece of code that produces a value – 7, price, price * qty, "Hi " + name – is called an expression, and expressions can be combined into larger ones. Most operators take two operands (they are binary); a few, such as ! and typeof, take only one (they are unary); and the ternary operator ? : takes three.

Knowing these words will help you read error messages and documentation, which use them constantly.

The + operator and strings

When either side of + is a string, JavaScript joins (concatenates) instead of adding.

Example JAVASCRIPT
console.log("Nai" + "robi");
console.log("Room " + 12);
console.log(1 + 2 + "3");
console.log("1" + 2 + 3);
Result
Nairobi
Room 12
33
123
Try it Yourself »

+ works left to right. In the third line 1 + 2 is added first to make 3, then joined with "3". In the last line the string comes first, so everything after it is joined.

The other arithmetic operators (-, *, /) always try to convert to numbers, so "10" - 4 is 6. Relying on that is confusing – convert explicitly with Number() instead.

Comparison operators

Comparisons produce true or false.

OperatorMeaning
===equal value and equal type
!==not equal value or type
==equal after type conversion
!=not equal after type conversion
> / <greater than / less than
>= / <=greater or equal / less or equal

They are covered in depth in Booleans & Comparisons.

Logical operators

OperatorNameTrue when
&&ANDboth sides are true
||ORat least one side is true
!NOTflips true to false and back
Example JAVASCRIPT
const age = 20;
const hasId = true;

console.log(age >= 18 && hasId);
console.log(age < 13 || age > 65);
console.log(!hasId);
Result
true
false
false
Try it Yourself »

Nullish coalescing and optional chaining

Two modern operators make working with missing values much safer.

?? returns the right-hand value only when the left is null or undefined. ?. reads a property only if the thing before it exists, returning undefined instead of crashing.

Example JAVASCRIPT
const settings = { theme: null, user: { name: "Mutua" } };

console.log(settings.theme ?? "light");
console.log(settings.user?.name);
console.log(settings.admin?.name);
Result
light
Mutua
undefined
Try it Yourself »
Note: ?? differs from ||: 0 || 5 gives 5 because 0 counts as false, but 0 ?? 5 gives 0 because 0 is a real value, not a missing one.
Exercise

Use the operator that gives the remainder of a division.

const leftOver = 23  4;
Exercise

Add 50 to points using a shorter assignment operator.

points  50;
Sign in or create a free account to track progress, earn XP, badges and a certificate.