Numbers and maths · ตัวเลขและคณิตศาสตร์
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ SharedArrayBuffer) กรุณาอัปเดต Chrome, Edge, Firefox หรือ Safari เป็นเวอร์ชันล่าสุด
English
Maths like a calculator
- JavaScript does maths with
+-*/. - There is only one kind of number, so
/can give a decimal:7 / 2is3.5.
ไทย
คณิตศาสตร์เหมือนเครื่องคิดเลข
- JavaScript ทำการคำนวณ
+-*/ - มีประเภทตัวเลขเพียงชนิดเดียว ดังนั้น
/จึงสามารถให้ค่า ทศนิยม:7 / 2คือ3.5
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.
ไทย
ผลหารเศษและเลขยกกำลัง
%ให้ เศษเหลือ หลังการหาร:23 % 5คือ3(5 เข้าไปใน 4 จำนวน 3 ครั้ง มีเศษเหลือ)**ทำการ เลขยกกำลัง:2 ** 5คือ2 × 2 × 2 × 2 × 2
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.
ไทย
ลำดับความสำคัญของการดำเนินการ
*/%เกิดขึ้น ก่อน+และ-— ใช้กฎเดียวกับในชั้นเรียนคณิตศาสตร์- ใช้วงเล็บ
( )เพื่อบังคับให้ดำเนินการส่วนนั้นเกิดขึ้นก่อน
Explore · สำรวจ
Order of operations, step by step · ลำดับการดำเนินการ, ขั้นตอน-by-step
× and ÷ happen before + and −. Brackets go first of all. · × และ ÷ เกิดขึ้น ก่อน + และ −. วงเล็บมาก่อนที่สุด.
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.
ไทย
ข้อผิดพลาดที่พบบ่อย
%เป็นตัวดำเนินการผลหารเศษ;/สามารถให้ค่าทศนิยมได้"3" + 4ให้ค่า"34"(สตริง), ไม่ใช่7
English
Now you try
- Use the maths operators.
- Press Check answer to test it.
ไทย
ลองดูเลย
- ใช้ตัวดำเนินการทางคณิตศาสตร์
- กด Check answer เพื่อทดสอบ
Log 6 times 7 (it is 42). · Log 6 คูณ 7 (คือ 42).
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Use % to log the remainder when 23 is divided by 5 (it is 3). · ใช้ % เพื่อ log เศษเหลือเมื่อ 23 หารด้วย 5 (คือ 3).
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
a is 4 and b is 8. Log their average, (a + b) / 2 (it is 6). Use brackets so the addition happens first. · a คือ 4 และ b คือ 8. Log ค่าเฉลี่ยของพวกมัน, (a + b) / 2 (คือ 6). ใช้วงเล็บเพื่อให้การบวกเกิดขึ้นก่อน.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่