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

Contenido del Curso

Introduction to JavaScript

Introduction to JavaScript

1. Basic Concepts
2. Variables and Data Types
3. Basic Operations
4. Conditional Statements
5. Loops
6. Functions

bookMathematical Operations

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.

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 2
We're sorry to hear that something went wrong. What happened?
some-alt