Canddi

What are the different javascript operators?

JavaScript operators let you calculate values, update variables and test conditions. This beginner guide covers common groups with examples you can run in a browser console. It is a starting point, rather than a complete language reference.

Arithmetic and assignment operators

For numbers, +, -, * and / perform addition, subtraction, multiplication and division. % returns a remainder; ** raises a value to a power. The same + symbol can join strings.

console.log(17 % 5); // 2
console.log(3 ** 3); // 27
console.log("Level " + 4); // Level 4

= assigns a value. Compound assignment combines an operation with an update, as in score += 3. For a fuller explanation, see our compound-operators guide.

Increment and decrement

++ increases a variable by one, while -- decreases it by one. Position matters when you use the expression’s result: postfix count++ returns the value before the update; prefix ++count returns the updated value. The MDN increment reference explains this distinction.

let count = 4;
console.log(count++); // 4
console.log(count); // 5
console.log(++count); // 6

When learning, keep updates on their own line unless you specifically need the returned value. This makes it easier to follow the sequence.

Comparison operators

Comparisons return true or false. < means less than, > means greater than, and <= and >= include equality.

Strict equality, ===, compares without converting different types to match. !== is strict inequality. By contrast, == and != can perform type conversion. Prefer strict comparisons when conversion is not part of your intended logic.

console.log(4 < 7); // true
console.log(7 >= 7); // true
console.log(4 === "4"); // false
console.log(4 == "4"); // true

Strict equality is not a deep comparison of object contents: two separately created objects are different even if their properties look the same. Also, NaN === NaN is false. These details matter when you move beyond simple numbers and strings.

Logical operators and defaults

With Boolean values, && means AND, || means OR, and ! means NOT. Write the two vertical bars of || together, without a space between them.

const hasTicket = true;
const isCancelled = false;
console.log(hasTicket && !isCancelled); // true

Logical AND and logical OR short-circuit: they may skip evaluating the right-hand expression. They return an operand, which is not necessarily a Boolean. For example, "ready" && 5 returns 5.

The nullish coalescing operator, ??, supplies a fallback only for null or undefined. This differs from ||, which also replaces values such as zero and an empty string.

console.log(0 || 10); // 10
console.log(0 ?? 10); // 0
console.log(null ?? 10); // 10

Choosing values and controlling order

The conditional operator condition ? valueA : valueB chooses between two expressions. Parentheses clarify grouping; multiplication normally takes priority over addition.

const places = 2;
console.log(places > 0 ? "Open" : "Full"); // Open
console.log(2 + 3 * 4); // 14
console.log((2 + 3) * 4); // 20

Other groups include bitwise operators and operators such as typeof. Use the MDN expressions and operators guide for the wider reference.

Practise with a small example

Predict both outputs, then run the code. Change seats to zero and explain why the result changes.

let seats = 3;
seats -= 1;
const canBook = seats > 0;
console.log(seats); // 2
console.log(canBook); // true

When an answer surprises you, check:

  • Are your values numbers, strings or something else?
  • Did you assign a value or compare it?
  • Does grouping change the calculation?
  • Did a previous statement update the variable?

Build This Skill

For guided practice, explore Academy Class’s JavaScript Beginner course. Review the current outline and entry guidance to choose the right starting point.