Numbers and maths · Nombres et mathématiques
Maths like a calculator
- JavaScript does maths with
+-*/. - There is only one kind of number, so
/can give a decimal:7 / 2is3.5.
Mathématiques comme une calculatrice
- JavaScript effectue des mathématiques avec
+-*/. - Il n'existe qu'un seul type de nombre, donc
/peut donner un décimal :7 / 2est3.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.
Reste et puissances
%donne le reste après la division :23 % 5est3(5 contient 4 fois, 3 reste).**élève à une puissance :2 ** 5est2 × 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.
Ordre des opérations
*/%se font avant+et-— les mêmes règles qu'en cours de maths.- Utilisez des parenthèses
( )pour forcer ce qui doit se faire en premier.
Order of operations, step by step · Ordre des opérations, étape par étape
× and · et ÷ happen before · avant + and · et −. Brackets go first of all. · × et ÷ se produisent avant + et −. Les parenthèses passent en premier.
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.
Erreurs courantes
%est l'opérateur de reste ;/peut donner un décimal."3" + 4retourne"34"(une chaîne), pas7.
Now you try
- Use the maths operators.
- Press Check answer to test it.
À vous maintenant
- Utilisez les opérateurs mathématiques.
- Appuyez sur Vérifier la réponse pour tester.
Log 6 times 7 (it is 42). · Log 6 fois 7 (c'est 42).
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Use % to log the remainder when 23 is divided by 5 (it is 3). · Utilisez % pour journaliser le reste lorsque 23 est divisé par 5 (c'est 3).
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
a is 4 and · et b is 8. Log their average, (a + b) / 2 (it is 6). Use brackets so the addition happens first. · a est 4 et b est 8. Journalisez leur moyenne, (a + b) / 2 (c'est 6). Utilisez des parenthèses pour que l'addition se produise en premier.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.