JavaScriptLesson 3 of 18

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.

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.

JS
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.

JS
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".

JS
typeof 42;      // "number"
typeof "42";    // "string"
typeof true;    // "boolean"

Syntax

JS
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.

JS
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.
Try it Yourself Edit the code, press Run, then check what changed in the Result.
Ready
Code
Result

Practice makes it stick

Open the Playground and build your own page with the tags you just learned.

→ Open Playground