Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Mathematical Operations in JavaScript | Performing Operations in JavaScript
Introduction to JavaScript

bookMathematical Operations in JavaScript

メニューを表示するにはスワイプしてください

JavaScript can perform the following operations with numbers:

  • Addition (+);
  • Subtraction (-);
  • Multiplication (*);
  • Division (/);
  • Remainder, or Modulo (%);
  • Exponent (**).

Note

If you are already familiar with these operations and how they work, skip to the last section (Priority of Execution of Operations) or proceed with this chapter.

Addition and Subtraction

123456
console.log(25 + 13); // Addition console.log(37 - 2); // Subtraction let a = 25, b = 23; console.log(a + b); // Addition console.log(a - b); // Subtraction
copy

Multiplication and Division

1234567
console.log(12 * 3); // Multiplication console.log(12 / 3); // Division console.log(273 / 23); // Division let a = 77, b = 11; console.log(a * b); // Multiplication console.log(a / b); // Division
copy

Remainder (Modulo)

This operation returns the remainder of a division and is performed using the % operator:

12345
console.log(77 % 10); console.log(25 % 11); let a = 27, b = 21; console.log(a % b);
copy

Exponent

This operation raises a number to a certain power. The first number is the base, and the second is the exponent to which it must be raised. It is performed using the ** operator:

123456
console.log(10 ** 6); // 10 * 10 * 10 * 10 * 10 * 10 (6 times) console.log(2 ** 7); // 2 * 2 * 2 * 2 * 2 * 2 * 2 (7 times) let a = 2; let b = 3; console.log(a ** b);
copy

Priority of Execution of Operations

Each operation has its execution priority, and the sequence of execution depends on it.

Note

If operations have the same priority, they will be executed from left to right.

You can use parentheses ( ) to modify the priority of execution:

123
console.log(25 + 7 * 2 ** 3); // Example 1 console.log((25 + 7) * 2 ** 3); // Example 2 console.log(((25 + 7) * 2) ** 3); // Example 3
copy

Note

Parentheses () have the highest priority. Inner parentheses are evaluated first, followed by outer ones.

1. What does the % operator return in JavaScript?

2. What will be the result of the following expression?

question mark

What does the % operator return in JavaScript?

正しい答えを選んでください

question mark

What will be the result of the following expression?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 3.  2

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 3.  2
some-alt