๐ŸŽ“ A1Learner
๐Ÿ”
0 of 55 lessons completed

JavaScript Operators

An operator is a symbol that does a job on one or two values. Arithmetic operators work on numbers, and assignment operators store results.

Arithmetic results

Each line runs one arithmetic operator on the same two numbers. The label names the result on each line.

console.log("Add:", 10 + 4);
console.log("Subtract:", 10 - 4);
console.log("Multiply:", 10 * 4);
console.log("Divide:", 10 / 4);

Remainder and power

The first line leaves the remainder of a division. The second line raises 2 to the power of 3.

console.log("Remainder:", 10 % 3);
console.log("Power:", 2 ** 3);

Growing a value

The += operator adds 5 to count and stores the result. The log line then shows the new value.

let count = 10;
count += 5;
console.log(count);

Joining text

The + operator joins two pieces of text here. The result is one piece of text with no space between the parts.

console.log("Java" + "Script");

Example

let score = 20;
score += 10;
console.log("After bonus:", score);
score++;
console.log("After step up:", score);
score--;
console.log("After step down:", score);
console.log("Half:", score / 2);

Key points

  • Each operator takes values and produces a result.
  • The % operator gives the remainder after division.
  • The ** operator raises a number to a power.
  • The ++ operator adds one, and -- takes one away.
  • Assignment operators like += add a value and store the result in one step.
  • Bitwise operators act on the binary bits inside numbers and are rare.

In this example, score starts at 20. The += operator adds 10 and stores 30. The ++ operator raises the value to 31, and -- lowers it back to 30. The last line divides the final value by 2 and prints 15.

Try it Yourself

Edit the code, then press Run.

Runs in your browser โ€” the preview under the editor is the real result. Nothing is sent to a server. The code runner panel (right) does the same with more room.

Exercise

What does 17 % 5 give?