Numbers and maths
עמוד זה דורש דפדפן עדכני (תומך ב-SharedArrayBuffer). אנא עדכן את Chrome, Edge, Firefox או Safari לגרסה העדכנית ביותר.
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.
חקור
Order of operations, step by step
× וגם ÷ happen לפני + וגם −. Brackets go first of all.
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.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Log 6 times 7 (it is 42).
לחץ על הרץ כדי לראות את התוצא כאן.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Use % to log the remainder when 23 is divided by 5 (it is 3).
לחץ על הרץ כדי לראות את התוצא כאן.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
a הוא 4 וגם b הוא 8. Log their average, (a + b) / 2 (it is 6). Use brackets so the addition happens first.
לחץ על הרץ כדי לראות את התוצא כאן.