JavaScript Operators

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.

6 min read Level 1/5 #operators#arithmetic#comparison
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.

Arithmetic operators script.js
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)
▶ Preview: console
OperatorNameExampleResult
+addition5 + 27
-subtraction5 - 23
*multiplication5 * 210
/division10 / 42.5
%remainder10 % 42
**exponentiation2 ** 38

String Concatenation With +

The + operator does double duty: it adds numbers AND joins strings.

+ joins strings script.js
const first = "Hello, ";
const second = "world!";
console.log(first + second); // "Hello, world!"
▶ Preview: console

When one side is a string and the other is a number, the number gets converted to a string:

String + number = string script.js
console.log("Total: " + 42); // "Total: 42"
console.log("5" + 3);        // "53"  ← not 8!
console.log(5 + "3");        // "53"
▶ Preview: console

Comparison

Comparison operators ask a question and return true or false.

Comparison operators script.js
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
▶ Preview: console
OperatorMeaning
===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.

Loose equality is surprising script.js
console.log(5 == "5");  // true  — converts the string to a number
console.log(0 == "");   // true  — both are "falsy"
console.log(null == undefined); // true
▶ Preview: console

Assignment Shortcuts

You’ll often want to update a variable based on its current value. JavaScript has shortcuts for this.

Assignment shortcuts script.js
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
▶ Preview: console

Increment and Decrement

To add or subtract one, use ++ and --.

++ and -- script.js
let count = 0;
count++;
count++;
count++;
console.log(count); // 3

count--;
console.log(count); // 2
▶ Preview: console

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.

Precedence script.js
console.log(2 + 3 * 4);   // 14, not 20
console.log((2 + 3) * 4); // 20
▶ Preview: console

Try It Yourself

Exercise

Compute a tax-inclusive price

Difficulty 1/5~2 min
Declare a constant `price` set to `100` and a constant `taxRate` set to `0.07`. Compute the tax-inclusive total in a variable called `total` and log it. The expected output is `107`.
solution.js
// your code here
4tests will run
💡 Show hint
`total = price + price * taxRate`. Use `const` for the inputs since they won't change.
✅ 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 →