JavaScript Tutorial

JavaScript Arithmetic

Add, subtract, multiply, divide, remainder, and increment. Watch the order of operations.

The arithmetic set

Arithmetic operators work on numbers. JavaScript has one number type for both integers and fractions, so10 / 4 is 2.5. You do not switch to a separate float type.

OperatorNameExampleResult
+add7 + 29
-subtract7 - 25
*multiply7 * 214
/divide7 / 23.5
%remainder7 % 21
**exponent2 ** 38

Add, subtract, multiply, divide

Write the numbers, the operator, and read the result with console.log. Store a result in a name when you need it again.

Example

const width = 12;
const height = 5;
const area = width * height;
const perimeter = 2 * (width + height);
console.log(area);
console.log(perimeter);
console.log(width / height);

Run this in /javascript/try. The console under the page prints the numbers. That editor is.

Remainder

% is not percent. It is the remainder after division. 10 % 3 is 1.10 % 2 is 0 — that is how you test even numbers. 10 % 10 is0. 3 % 10 is 3 because 10 does not fit into 3 even once.

Example

console.log(10 % 3);
console.log(10 % 2);
console.log(3 % 10);

const n = 14;
if (n % 2 === 0) {
  console.log(n + " is even");
} else {
  console.log(n + " is odd");
}

Remainder is also how a counter wraps. If you have 5 slides and the index is i, the next index is (i + 1) % 5. After 4 comes 0.

Increment and decrement

++ adds one. -- subtracts one. Written as its own statement, n++ and++n both change n by one. This tutorial uses them on their own line so prefix and postfix do not matter yet.

After let n = 5;, the statement n++; leaves n equal to 6.n--; would take it back to 5. You can also write n += 1 if you want the change to look like assignment.

Example

let count = 0;
count++;
count++;
console.log(count);
count--;
console.log(count);

Do not write const count = 0; count++;. const cannot be reassigned. Increment needs let.

Order of operations

Exponent runs first, then multiply, divide, and remainder, then add and subtract. Same-level operators run left to right. Parentheses always win. When a formula is a real calculation — tax, a total, a remaining budget — put parentheses around the groups you mean, even if the default order already matches.

Example

console.log(2 + 3 * 4);
console.log((2 + 3) * 4);
console.log(2 ** 3 * 2);
console.log(10 - 4 - 3);
console.log(10 - (4 - 3));

2 + 3 * 4 is 14. (2 + 3) * 4 is 20.2 ** 3 * 2 is 16 because the power runs first. 10 - 4 - 3 is3 (left to right). 10 - (4 - 3) is 9.

Unary plus and minus

A minus in front of a value negates it: -n flips the sign. A plus in front of a string tries to turn that string into a number: +"42" is 42. If the string is not numeric,+"hello" is NaN (Not a Number). Prefer Number("42") when you convert on purpose — it reads as a conversion, not as arithmetic.

Mixing strings and numbers

+ concatenates if either side is a string. The other arithmetic operators coerce strings to numbers instead. "10" - 3 is 7. "10" * 3 is 30."10" + 3 is "103". That split is why totals go wrong when a form value is still text.

Form fields give you strings. Convert before you add: Number(input.value) orparseFloat(input.value). Then + adds.

Next: assignment — putting a value in a name, and the shortcuts that change the value already there.

FAQ: JavaScript Arithmetic

Common questions about this page.

What is the StudyGrid JavaScript tutorial?

The StudyGrid JavaScript tutorial is a full beginner track: variables, functions, arrays, if/else, the DOM, events, storage, and fetch. Each chapter has copy-and-run examples.

Should I run javascript arithmetic examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn javascript arithmetic in this JavaScript JavaScript lesson (JavaScript Arithmetic).

Is the JavaScript editor the same as Try Python?

No. Try JavaScript is a live page plus a console at /javascript/try. Try Python stays at /try. JavaScript lessons never open the Python editor.

Do I need to install anything to learn JavaScript?

No. Open a chapter, click Try it in JavaScript, and the page and console update in the browser.

Where should I start the JavaScript tutorial?

Start at JavaScript Intro, then Where To, Output, and Syntax. After variables and functions, continue to the DOM and events. Use Next at the bottom of each chapter.

Is the JavaScript tutorial free?

Yes. The JavaScript studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live editor.