TExcelTAD
Home › JavaScript › JS Operators

JavaScript Operators

An operator takes one or more values (its operands) and produces a new value. You've already met = and +. This chapter is a map of all the main operator families; the next chapters look at arithmetic and assignment in more depth.

Operator families

Family Operators Example Result
Arithmetic + - * / % ** 7 % 3 1
Assignment = += -= *= /= ??= … x += 2 adds 2 to x
Comparison == === != !== > < >= <= 5 >= 5 true
Logical && || ! true && false false
Nullish ?? null ?? "default" "default"
String + += "a" + "b" "ab"
Conditional (ternary) cond ? a : b age >= 18 ? "adult" : "minor" one of two values
Type typeof instanceof typeof 42 "number"
Optional chaining ?. user?.address undefined if user is null

Arithmetic and strings

js · try it yourself
console.log(10 + 4, 10 - 4, 10 * 4, 10 / 4);
console.log(10 % 4, 2 ** 5);
console.log("Hello" + ", " + "world");
console.log("Total: " + 3 + 4);     // string + number -> string, left to right
console.log("Total: " + (3 + 4));   // parentheses first
14 6 40 2.5
2 32
Hello, world
Total: 34
Total: 7

When + sees a string on either side, it joins rather than adds. Evaluation goes left to right, so "Total: " + 3 becomes "Total: 3", and then + 4 appends "4".

Comparison

Comparisons produce true or false. Always prefer the strict operators === and !==, which compare both value and type:

js · try it yourself
console.log(5 === 5);     // same value, same type
console.log(5 === "5");   // number vs string
console.log(5 == "5");    // loose: converts "5" to 5 first
console.log(3 !== 4);
console.log("apple" < "banana");  // strings compare alphabetically
true
false
true
true
true

Logical operators

&& (and) is true only if both sides are true; || (or) if at least one is; ! (not) flips a boolean.

js · try it yourself
const age = 25;
const hasTicket = true;

console.log(age >= 18 && hasTicket);   // both true
console.log(age < 12 || hasTicket);    // one is true
console.log(!hasTicket);
true
true
false

&& and || actually return one of their operands, which makes them handy for defaults. ?? is a stricter version of || that only falls back on null or undefined — not on 0 or "":

js · try it yourself
const volume = 0;
console.log(volume || 50);   // 0 is "falsy", so || picks 50
console.log(volume ?? 50);   // 0 is not null/undefined, so ?? keeps it

const nickname = null;
console.log(nickname ?? "Anonymous");
50
0
Anonymous

The ternary operator

condition ? valueIfTrue : valueIfFalse is a compact if…else that produces a value:

js · try it yourself
const stock = 0;
const label = stock > 0 ? "In stock" : "Sold out";
console.log(label);
Sold out

Operator precedence

When an expression mixes operators, precedence decides which runs first — just like in maths, * binds tighter than +. From highest to lowest (simplified):

Level Operators
Grouping ( … )
Member access / call . ?. [] ()
Unary ! typeof -x ++ --
Exponent **
Multiplicative * / %
Additive + -
Relational < <= > >=
Equality === !== == !=
Logical AND &&
Logical OR / nullish || ??
Conditional ? :
Assignment = += …

Tip: When in doubt, add parentheses. (a + b) * c is clearer than relying on a precedence table, and costs nothing.

Test yourself

What does "5" + 2 evaluate to?

  1. 7
  2. "52"
  3. "7"
  4. NaN

What does 0 ?? 10 evaluate to?

  1. 0
  2. 10
  3. null
  4. false

Which comparison is false?

  1. 5 == "5"
  2. 5 === 5
  3. 5 === "5"
  4. 5 !== "5"