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

book
Mathematical 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

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

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
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:

console.log(77 % 10);
console.log(25 % 11);

let a = 27, b = 21;
console.log(a % b);
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:

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);
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:

console.log(25 + 7 * 2 ** 3); // Example 1
console.log((25 + 7) * 2 ** 3); // Example 2
console.log(((25 + 7) * 2) ** 3); // Example 3
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?

Select the correct answer

question mark

What will be the result of the following expression?

console.log(25 + 7 * 2 ** 3);

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 2

Pregunte a AI

expand
ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

We use cookies to make your experience better!
some-alt