JavaScript Tutorial
JavaScript Operators
Operators compute and compare: +, -, *, /, %, ===, &&, ||, and the rest of the everyday set.
What an operator does
An operator is a symbol that takes one or two values and produces a result. 3 + 4 uses+ to add. score === 10 uses === to compare.loggedIn && isAdmin uses && to combine two yes-or-no values.
You already used operators when you wrote let n = 5. The = on that line is assignment: it stores a value in a name. The rest of this chapter is the everyday set you will type in almost every script.
Arithmetic
+, -, *, and / add, subtract, multiply, and divide.% is remainder: what is left after division. Unlike some languages, JavaScript division on two integers can still produce a fraction: 10 / 4 is 2.5, not 2.
Example
console.log(10 + 3);
console.log(10 - 3);
console.log(10 * 3);
console.log(10 / 4);
console.log(10 % 3);
10 % 3 is 1: three fits into ten three times, remainder one. Remainder is how you test even and odd (n % 2 === 0) and how you wrap a counter back to zero.
Click Try it in JavaScript under an example. That opens /javascript/try — a live page and a console.
Comparison
Comparisons produce true or false. Prefer === (equal value and equal type) over == (equal after type conversion). "5" == 5 is true;"5" === 5 is false. A later chapter covers comparisons in depth. Use === from the start.
| Operator | Meaning |
|---|---|
=== | equal value and type |
!== | not equal value or type |
== | equal after conversion (avoid) |
!= | not equal after conversion (avoid) |
< | less than |
> | greater than |
<= | less than or equal |
>= | greater than or equal |
Example
const score = 10;
console.log(score === 10);
console.log(score !== 0);
console.log(score > 5);
console.log("5" === 5);
console.log("5" == 5);
A single = assigns. It does not compare. Writing if (score = 10) stores 10 inscore and then treats that value as true. Always compare with ===.
Logic
&& is and: both sides must be true. || is or: at least one side is true.! is not: it flips true and false. These operators work on any value, but this chapter uses booleans so the result is obvious.
Example
const loggedIn = true;
const isAdmin = false;
console.log(loggedIn && isAdmin);
console.log(loggedIn || isAdmin);
console.log(!isAdmin);
loggedIn && isAdmin is false because one side is false.loggedIn || isAdmin is true because one side is true. !isAdmin istrue.
Assignment and shortcuts
= stores a value. +=, -=, *=, /=, and%= update the value that is already there. n += 2 means n = n + 2. The next chapter is all arithmetic; the one after that is all assignment. Here you only need the idea that= writes and === asks.
Plus does two jobs
With numbers, + adds. With strings, + concatenates: it glues text together. If one side is a string, JavaScript turns the other side into a string and concatenates. That surprise is the most common operator bug for beginners.
Example
console.log(2 + 3);
console.log("2" + 3);
console.log("Hello, " + "Mina");
console.log(2 + 3 + "px");
console.log("px" + 2 + 3);
2 + 3 is 5. "2" + 3 is "23". Left to right,2 + 3 + "px" adds first (5) then concatenates ("5px")."px" + 2 + 3 concatenates all the way: "px23".
Order of operations
Multiplication and division run before addition and subtraction. Parentheses change the order. When a line mixes arithmetic, comparison, and logic, write parentheses so a reader (and you, next week) can see the grouping without memorizing a table.
2 + 3 * 4 is 14, not 20. (2 + 3) * 4 is 20.n > 0 && n < 10 is already clear. n > 0 && n < 10 || skipis not: add parentheses.
Next: arithmetic in detail — remainder, increment, and the order of operations with numbers only.