JavaScript Operators

Operators are the symbols that let you combine, compare, and transform values to produce new results.

What are operators?

An operator performs an action on one or more values, which are called operands. In the expression 5 + 3, the plus sign is the operator and 5 and 3 are its operands. Operators are how you actually do work in a program: adding prices, checking whether a password matches, or deciding which branch of code should run. JavaScript groups its operators into several families, each with a clear job.

  • Arithmetic operators perform math such as addition and multiplication.
  • Assignment operators store values in variables, often while doing math at the same time.
  • Comparison operators compare two values and return a boolean.
  • Logical operators combine boolean expressions to build conditions.
  • String operators join text together using the plus sign.
  • Ternary operator chooses between two values based on a condition.

Unary, binary, and ternary

Operators are also described by how many operands they take. A unary operator works on a single value, like the negation in -x. A binary operator works on two values, like a + b, and these are by far the most common. The ternary operator is the only one that takes three parts and is a compact way to write a simple if...else decision.

The three operator arities

<!DOCTYPE html>
<html>
<body>

<h2>The three operator arities</h2>

<script>
let n = 5;

let negative = -n;        // unary: one operand
let total = n + 10;       // binary: two operands
let label = n > 0 ? "positive" : "not positive"; // ternary: three parts

console.log(negative); // -5
console.log(total);    // 15
console.log(label);    // "positive"
</script>

</body>
</html>

Operator precedence

When several operators appear in one expression, precedence decides the order in which they run, just like in ordinary math. Multiplication and division happen before addition and subtraction, and you can use parentheses to force a different order. Making the order explicit with parentheses is a good habit because it makes your intent obvious to anyone reading the code.

Precedence and grouping

<!DOCTYPE html>
<html>
<body>

<h2>Precedence and grouping</h2>

<script>
let a = 2 + 3 * 4;     // 14, because * runs before +
let b = (2 + 3) * 4;   // 20, parentheses run first

let fullName = "Grace" + " " + "Hopper"; // string joining

console.log(a);        // 14
console.log(b);        // 20
console.log(fullName); // "Grace Hopper"
</script>

</body>
</html>
CategoryExamplePurpose
Arithmetica + bPerform math on numbers
Assignmenta = bStore a value in a variable
Comparisona === bCompare two values, return boolean
Logicala && bCombine boolean conditions
String"a" + "b"Join text together
Ternarya ? b : cChoose one of two values
Note: You do not need to memorize the full precedence table. When in doubt, add parentheses to state the order you want. Clear code beats clever code.

Exercise: JavaScript Operators

What does the === operator check that == does not?