Números y matemáticas
Esta página necesita un navegador reciente (con soporte para SharedArrayBuffer). Por favor, actualiza Chrome, Edge, Firefox o Safari a la última versión.
English
Maths like a calculator
- JavaScript does maths with
+-*/. - Everyday numbers are all one type,
number(a separateBigIntexists for huge whole numbers), so/can give a decimal:7 / 2is3.5.
English
console.log(7 + 2); // → 9
console.log(7 / 2); // → 3.5
English
Remainder and powers
%gives the remainder after dividing:23 % 5is3(5 goes in 4 times, 3 left over).**raises to a power:2 ** 5is2 × 2 × 2 × 2 × 2.
English
console.log(23 % 5); // → 3
console.log(2 ** 5); // → 32
English
Order of operations
*/%happen before+and-— the same rules as in maths class.- Use brackets
( )to force what should happen first.
Explorar
Orden de las operaciones, paso a paso
× y ÷ se ejecutan antes que + y −. Los paréntesis van primero.
English
console.log(2 + 3 * 4); // → 14 (the 3 * 4 first)
console.log((2 + 3) * 4); // → 20 (the brackets first)
English
Common mistakes
%is the remainder operator;/can give a decimal."3" + 4gives"34"(a string), not7.
English
Now you try
- Use the maths operators.
- Press Check answer to test it.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Imprime 6 por 7 (es 42).
Haz clic en Ejecutar para ver la salida aquí.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Usa % para imprimir el resto cuando 23 se divide por 5 (es 3).
Haz clic en Ejecutar para ver la salida aquí.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
a es 4 y b es 8. Imprime su promedio, (a + b) / 2 (es 6). Usa paréntesis para que la suma ocurra primero.
Haz clic en Ejecutar para ver la salida aquí.