Numbers and maths · Números y matemáticas
Maths like a calculator
- JavaScript does maths with
+-*/. - There is only one kind of number, so
/can give a decimal:7 / 2is3.5.
Matemáticas como una calculadora
- JavaScript realiza operaciones matemáticas con
+-*/. - Solo existe un tipo de número, por lo que
/puede devolver un decimal:7 / 2da como resultado3.5.
console.log(7 + 2); // → 9
console.log(7 / 2); // → 3.5
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.
Resto y potencias
%devuelve el resto de una división:23 % 5es3(5 cabe 4 veces y sobran 3).**eleva a una potencia:2 ** 5equivale a2 × 2 × 2 × 2 × 2.
console.log(23 % 5); // → 3
console.log(2 ** 5); // → 32
Order of operations
*/%happen before+and-— the same rules as in maths class.- Use brackets
( )to force what should happen first.
Orden de las operaciones
*/%se ejecutan antes que+y-— siguiendo las mismas reglas que en clase de matemáticas.- Usa paréntesis
( )para forzar el orden de ejecución.
Order of operations, step by step · Orden de las operaciones, paso a paso
× and · y ÷ happen before · antes + and · y −. Brackets go first of all. · × y ÷ se ejecutan antes que + y −. Los paréntesis van primero.
console.log(2 + 3 * 4); // → 14 (the 3 * 4 first)
console.log((2 + 3) * 4); // → 20 (the brackets first)
Common mistakes
%is the remainder operator;/can give a decimal."3" + 4gives"34"(a string), not7.
Errores comunes
%es el operador de resto;/puede dar un decimal."3" + 4da como resultado"34"(una cadena de texto), no7.
Now you try
- Use the maths operators.
- Press Check answer to test it.
Ahora te toca a ti
- Utiliza los operadores matemáticos.
- Pulsa Comprobar respuesta para verificarlo.
Log 6 times 7 (it is 42). · Imprime 6 por 7 (es 42).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Use % to log the remainder when 23 is divided by 5 (it is 3). · Usa % para imprimir el resto cuando 23 se divide por 5 (es 3).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
a is 4 and · y b is 8. Log their average, (a + b) / 2 (it is 6). Use brackets so the addition happens first. · a es 4 y b es 8. Imprime su promedio, (a + b) / 2 (es 6). Usa paréntesis para que la suma ocurra primero.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.