7. JavaScript Operators
What you will learn
- Arithmetic, assignment, comparison, and logical operators
- The difference between
==and=== - Operator precedence
- The conditional (ternary) operator
What is an operator?
An operator is a symbol that does something with values, called operands. In 10 + 5, the + is the operator and 10 and 5 are the operands.
Arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
Add | 7 + 3 |
10 |
- |
Subtract | 7 - 3 |
4 |
* |
Multiply | 7 * 3 |
21 |
/ |
Divide | 7 / 2 |
3.5 |
% |
Remainder | 7 % 3 |
1 |
** |
Power | 2 ** 3 |
8 |
++ |
Add one | x++ |
x grows by 1 |
-- |
Subtract one | x-- |
x shrinks by 1 |
Example
<p id="demo"></p>
<script>
const a = 17;
const b = 5;
document.getElementById("demo").innerHTML =
"a + b = " + (a + b) + "<br>" +
"a / b = " + (a / b) + "<br>" +
"a % b = " + (a % b) + " (remainder)<br>" +
"b ** 2 = " + (b ** 2);
</script>
Output (after the script runs)
a + b = 22
a / b = 3.4
a % b = 2 (remainder)
b ** 2 = 25
The remainder operator % is handy for checking even numbers: n % 2 === 0 is true when n is even.
Assignment operators
Shortcuts that change a variable using its own value.
| Operator | Same as |
|---|---|
x += 5 |
x = x + 5 |
x -= 5 |
x = x - 5 |
x *= 2 |
x = x * 2 |
x /= 2 |
x = x / 2 |
Example
<p id="demo"></p>
<script>
let x = 10;
x += 5; // 15
x *= 2; // 30
x -= 4; // 26
x++; // 27
document.getElementById("demo").textContent = "x is " + x;
</script>
Output (after the script runs)
x is 27
Comparison operators
Comparisons give a boolean result: true or false.
| Operator | Meaning |
|---|---|
=== |
Equal value and type (strict) |
!== |
Not equal (strict) |
== |
Equal after type conversion (loose) |
>, < |
Greater than, less than |
>=, <= |
Greater or equal, less or equal |
Always prefer === over ==
Example
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"5 == '5' is " + (5 == "5") + "<br>" +
"5 === '5' is " + (5 === "5") + "<br>" +
"5 !== '5' is " + (5 !== "5");
</script>
Output (after the script runs)
5 == ‘5’ is true
5 === ‘5’ is false
5 !== ‘5’ is true
The loose == converts types first, which produces surprising matches. The strict === compares the type as well, so it is safer and easier to predict.
Logical operators
| Operator | Name | Meaning |
|---|---|---|
&& |
AND | True only if both sides are true |
|| |
OR | True if at least one side is true |
! |
NOT | Flips true and false |
Example
<p id="demo"></p>
<script>
const age = 20;
const hasTicket = false;
document.getElementById("demo").innerHTML =
"Adult AND ticket: " + (age >= 18 && hasTicket) + "<br>" +
"Adult OR ticket: " + (age >= 18 || hasTicket) + "<br>" +
"NOT ticket: " + (!hasTicket);
</script>
Output (after the script runs)
Adult AND ticket: false
Adult OR ticket: true
NOT ticket: true
String operator
The + sign also joins strings. If either side is text, the result is text.
The conditional (ternary) operator
A short way to choose between two values:
Syntax
condition ? valueIfTrue : valueIfFalse
Example
<p id="demo"></p>
<script>
const age = 16;
const status = age >= 18 ? "adult" : "minor";
document.getElementById("demo").textContent = "You are a " + status;
</script>
Output (after the script runs)
You are a minor
Operator precedence
When an expression has several operators, JavaScript follows an order, like in maths. Multiplication and division come before addition and subtraction. Use parentheses to make your meaning clear.
Example
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"2 + 3 * 4 = " + (2 + 3 * 4) + "<br>" +
"(2 + 3) * 4 = " + ((2 + 3) * 4);
</script>
Output (after the script runs)
2 + 3 * 4 = 14
(2 + 3) * 4 = 20
Common mistakes
- Writing
=(assign) when you meant===(compare). - Using
==and being surprised by type conversion. - Forgetting that
+joins text when one side is a string. - Relying on precedence rules instead of adding parentheses.
- Expecting
/to give a whole number.7 / 2is 3.5.
Exercise
Using the remainder operator and the ternary operator, show even if number is even and odd if it is not. Then change the number to 7 and run it again.
Show solution
<p id="demo"></p>
<script>
const number = 14;
const result = number % 2 === 0 ? "even" : "odd";
document.getElementById("demo").textContent = number + " is " + result;
</script>
Knowledge check
1. What does 17 % 5 give?
2, the remainder of the division.
2. Why prefer === over ==?
It compares type as well as value, so it avoids surprising conversions.
3. What does && mean?
AND: true only if both sides are true.
Summary
- Arithmetic, assignment, comparison, and logical operators cover most needs.
- Use
===and!==for comparisons. - Parentheses make the order of operations clear.