ตัวเลขและคณิตศาสตร์
หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ 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.
สำรวจ
ลำดับการดำเนินการ, ขั้นตอน-by-step
× และ ÷ เกิดขึ้น ก่อน + และ −. วงเล็บมาก่อนที่สุด.
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 คูณ 7 (คือ 42).
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ใช้ % เพื่อ log เศษเหลือเมื่อ 23 หารด้วย 5 (คือ 3).
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
a คือ 4 และ b คือ 8. Log ค่าเฉลี่ยของพวกมัน, (a + b) / 2 (คือ 6). ใช้วงเล็บเพื่อให้การบวกเกิดขึ้นก่อน.
คลิก Run เพื่อดูผลลัพธ์ที่นี่