Free beginner tutorial

JavaScript Operators and Expressions

Operators calculate values, compare data, and combine conditions. Learn the common JavaScript operators and how to read the expressions they create.

Quick answer: An operator performs an action on one or more values. For example, + adds numbers, === checks strict equality, and && requires two conditions to be true.

Do arithmetic with numbers

Arithmetic operators are used for calculations. The remainder operator % returns what is left after division, which is useful for checks such as whether a number is even.

const subtotal = 24 + 16;
const half = subtotal / 2;
const remainder = 10 % 3;

console.log(subtotal); // 40
console.log(half);     // 20
console.log(remainder); // 1

Assign and update values

The = operator assigns a value. Compound assignment operators combine a calculation with assignment, which can make a repeated update more concise.

let score = 10;
score += 5; // same result as: score = score + 5
score *= 2; // same result as: score = score * 2

Compare values with strict equality

Comparison operators produce a Boolean value: true or false. Prefer === and !== for equality checks because they compare without converting values to another type first.

console.log(5 === 5);   // true
console.log(5 === "5"); // false
console.log(8 !== 3);   // true
console.log(8 >= 8);    // true

Use <, >, <=, and >= when comparing numeric ranges. Be deliberate when comparing text because string comparison follows character ordering rather than a person’s language rules.

Combine conditions with logical operators

&& is true when both sides are true, || is true when at least one side is true, and ! reverses a Boolean value.

const hasAccount = true;
const isVerified = false;
const canPublish = hasAccount && isVerified;
const canPreview = hasAccount || isVerified;
const needsLogin = !hasAccount;

JavaScript can stop evaluating a logical expression as soon as its result is known. This short-circuit behavior is often used to run a fallback or guard an action.

Use parentheses to make precedence clear

Some operators are evaluated before others. Parentheses make the intended grouping explicit and help prevent surprising results when expressions become more involved.

const total = 10 + 2 * 3;     // 16
const grouped = (10 + 2) * 3; // 36

Watch the plus operator with text

When either side of + is a string, JavaScript can join values as text instead of adding them as numbers. Convert user input before doing arithmetic.

const quantity = Number("4");
const total = quantity + 2; // 6
const label = "Items: " + quantity; // "Items: 4"

JavaScript operator questions

What is the difference between == and ===?

== may convert values before comparing them. === compares both value and type, so it usually makes the intended comparison clearer.

What does % mean in JavaScript?

It is the remainder operator. For example, 9 % 2 evaluates to 1 because one is left after dividing nine by two.

Practice: calculate a checkout total

Set a price and quantity, calculate the subtotal, and compare it with a free-shipping threshold. Use strict equality to check a status value, and combine the shipping conditions with a logical operator. Add parentheses anywhere they make your intended order easier to see.