Do Things With Values
JavaScript Operators
Operators perform work on values — add numbers, compare them, assign results to variables. The core arithmetic, comparison, and assignment operators you'll use in every program.
What you'll learn
- Use arithmetic operators correctly
- Choose between `===` and `==`
- Use assignment shortcuts like `+=` and `++`
- Understand operator precedence
An operator is a symbol that does something with values. You’ve
already met = (assignment) and + (addition). This lesson covers
the rest of the operators you’ll use every day.
Arithmetic
The math operators work the way you’d expect.
console.log(7 + 3); // 10
console.log(7 - 3); // 4
console.log(7 * 3); // 21
console.log(7 / 3); // 2.333...
console.log(7 % 3); // 1 ← remainder
console.log(2 ** 10); // 1024 ← exponent (ES2016) | Operator | Name | Example | Result |
|---|---|---|---|
+ | addition | 5 + 2 | 7 |
- | subtraction | 5 - 2 | 3 |
* | multiplication | 5 * 2 | 10 |
/ | division | 10 / 4 | 2.5 |
% | remainder | 10 % 4 | 2 |
** | exponentiation | 2 ** 3 | 8 |
String Concatenation With +
The + operator does double duty: it adds numbers AND joins strings.
const first = "Hello, ";
const second = "world!";
console.log(first + second); // "Hello, world!" When one side is a string and the other is a number, the number gets converted to a string:
console.log("Total: " + 42); // "Total: 42"
console.log("5" + 3); // "53" ← not 8!
console.log(5 + "3"); // "53" Comparison
Comparison operators ask a question and return true or false.
console.log(5 === 5); // true
console.log(5 === "5"); // false ← strict equality
console.log(5 !== 6); // true
console.log(5 > 3); // true
console.log(5 >= 5); // true
console.log(5 < 3); // false | Operator | Meaning |
|---|---|
=== | strictly equal |
!== | strictly not equal |
> | greater than |
< | less than |
>= | greater or equal |
<= | less or equal |
=== vs ==
You’ll also see == and != in older code. They do loose equality
— they try to convert types before comparing.
console.log(5 == "5"); // true — converts the string to a number
console.log(0 == ""); // true — both are "falsy"
console.log(null == undefined); // true Assignment Shortcuts
You’ll often want to update a variable based on its current value. JavaScript has shortcuts for this.
let n = 10;
n += 5; // same as: n = n + 5
console.log(n); // 15
n -= 3; // n = n - 3
console.log(n); // 12
n *= 2; // n = n * 2
console.log(n); // 24
n /= 4; // n = n / 4
console.log(n); // 6 Increment and Decrement
To add or subtract one, use ++ and --.
let count = 0;
count++;
count++;
count++;
console.log(count); // 3
count--;
console.log(count); // 2 You’ll see ++ most in for loops. Outside loops, prefer
count += 1 — it reads more clearly.
Operator Precedence
When you mix operators, multiplication and division happen before addition and subtraction — just like math class.
console.log(2 + 3 * 4); // 14, not 20
console.log((2 + 3) * 4); // 20 Try It Yourself
Exercise
Compute a tax-inclusive price
// your code here
💡 Show hint
✅ Show solution
const price = 100;
const taxRate = 0.07;
const total = price + price * taxRate;
console.log(total);
Up Next
That’s it for JS Basics. Now it’s time to look at the data shapes JavaScript gives you — strings, numbers, booleans, arrays.
JavaScript Data Types →