JavaScript Operators
Operators are symbols that do work on values. The plus sign adds numbers, the minus sign subtracts them, and the equals signs compare them. These are the same symbols you use in everyday math, with a few twists. Every program you write will lean on them heavily, so they are worth learning carefully.
Arithmetic is the easiest place to start. Use + to add, - to subtract, * to multiply, and / to divide. The % symbol returns the remainder of a division, so 10 % 3 gives 1. The twist: + also joins text. If either side is a string, "5" + 5 becomes "55", which is a classic beginner surprise.
Comparison operators answer questions: is the total over the limit? The result is always a boolean, true or false. Use === for strict equality, which compares both value and type, so 5 === "5" is false. The loose == converts types first, so 5 == "5" is true. In real code, === is the safer habit because == hides type mistakes.
Comparing values
Comparisons power decisions. Each one returns a boolean you can feed straight into an if statement. A common beginner mistake is using a single = in a comparison. That assigns a value instead of comparing, so the condition is always true.
const price = 20;
const budget = 25;
const affordable = price < budget; // true
Shortcut assignments
When you change a variable with math, combine the operator with the equals sign. The += operator adds and assigns in one step. The same pattern covers -=, *=, and /=, and they all keep your lines short.
let score = 0;
score += 10; // same as score = score + 10
score *= 2; // now 20
The typeof operator
typeof tells you the type of a value, which is handy when data arrives from a form or an API and you are not sure what you got. It returns strings like "number", "string", and "boolean".
typeof 42; // "number"
typeof "42"; // "string"
typeof true; // "boolean"
Syntax
const sum = 7 + 3; // 10
const remainder = 10 % 3; // 1
const strict = 5 === "5"; // false
const loose = 5 == "5"; // true
"5"+5 is "55", Not 10
Plus joins text if either side is string — "5"+5 becomes "55". Use Number("5")+5 or parseFloat.
Number("5")+5 // 10
Good to know
- + - * / do math; % gives the remainder.
- If either side of + is text, the result is text, not math.
- Use === for equality; == converts types first and can surprise you.
- Comparison results are always true or false.