JS กวดวิชา

เจเอสโฮม JS Introduction JS Where To JS เอาท์พุต งบ JS JS ไวยากรณ์ JS ความคิดเห็น ตัวแปร JS เจเอส เลท JS Const JS Operators JS เลขคณิต JS Assignment ประเภทข้อมูล JS ฟังก์ชัน JS วัตถุ JS JS Events JS Strings JS String Methods JS String Search เทมเพลตสตริง JS JS Numbers JS Number Methods JS Arrays JS Array Methods JS Array Sort JS Array Iteration JS Array Const JS Dates รูปแบบวันที่ JS JS วันที่รับเมธอด JS Date Set Methods JS Math JS สุ่ม JS Booleans JS เปรียบเทียบ JS Conditions JS Switch JS ลูปสำหรับ JS Loop สำหรับ In JS วนสำหรับ Of JS วนรอบในขณะที่ JS Break JS Iterables JS ชุด JS Maps JS Typeof JS ประเภทการแปลง JS Bitwise JS RegExp JS Errors JS ขอบเขต JS Hoisting JS โหมดเข้มงวด JS คำสำคัญนี้ JS Arrow Function JS Classes JS JSON JS Debugging คู่มือสไตล์ JS JS แนวทางปฏิบัติที่ดีที่สุด JS ความผิดพลาด JS Performance JS สงวนคำ

เวอร์ชัน JS

เวอร์ชัน JS JS 2009 (ES5) เจเอส 2015 (ES6) JS 2016 JS 2017 JS 2018 JS IE / ขอบ JS History

วัตถุ JS

คำจำกัดความของวัตถุ คุณสมบัติของวัตถุ วิธีการวัตถุ การแสดงวัตถุ อุปกรณ์เสริมวัตถุ ตัวสร้างวัตถุ ต้นแบบวัตถุ วัตถุ Iterables ชุดวัตถุ แผนที่วัตถุ การอ้างอิงวัตถุ

ฟังก์ชัน JS

คำจำกัดความของฟังก์ชัน พารามิเตอร์ฟังก์ชัน เรียกใช้ฟังก์ชัน เรียกฟังก์ชัน ฟังก์ชัน สมัคร การปิดฟังก์ชัน

JS Classes

แนะนำคลาส การสืบทอดคลาส คลาสคงที่

JS Async

JS Callbacks JS แบบอะซิงโครนัส JS Promises JS Async/รอ

JS HTML DOM

บทนำ DOM วิธี DOM เอกสาร DOM องค์ประกอบ DOM DOM HTML แบบฟอร์ม DOM DOM CSS ภาพเคลื่อนไหว DOM กิจกรรม DOM DOM Event Listener การนำทาง DOM โหนด DOM DOM Collections รายการโหนด DOM

JS เบราว์เซอร์ BOM

JS Window JS Screen JS Location JS History JS Navigator JS Popup Alert JS Timing JS Cookies

JS Web APIs

แนะนำ Web API เว็บฟอร์ม API API ประวัติเว็บ API ที่เก็บข้อมูลเว็บ Web Worker API API ดึงเว็บ เว็บ Geolocation API

JS AJAX

บทนำ AJAX AJAX XMLHttp คำขอ AJAX การตอบสนอง AJAX ไฟล์ AJAX XML AJAX PHP AJAX ASP ฐานข้อมูล AJAX แอปพลิเคชัน AJAX ตัวอย่าง AJAX

JS JSON

บทนำ JSON ไวยากรณ์ JSON JSON กับ XML ประเภทข้อมูล JSON JSON Parse JSON สตริงริฟาย วัตถุ JSON อาร์เรย์ JSON เซิร์ฟเวอร์ JSON JSON PHP JSON HTML JSON JSONP

JS กับ jQuery

ตัวเลือก jQuery jQuery HTML jQuery CSS jQuery DOM

JS กราฟิก

JS กราฟิก JS Canvas JS Plotly JS Chart.js JS Google Chart JS D3.js

ตัวอย่าง JS

ตัวอย่าง JS JS HTML DOM JS HTML อินพุต JS HTML Objects JS HTML Events JS Browser JS Editor JS แบบฝึกหัด JS Quiz ใบรับรอง JS

JS References

วัตถุ JavaScript วัตถุ HTML DOM


JavaScript วัตถุคณิตศาสตร์

ออบเจ็กต์ JavaScript Math ช่วยให้คุณทำงานทางคณิตศาสตร์กับตัวเลขได้

ตัวอย่าง

Math.PI;

วัตถุคณิตศาสตร์

ไม่เหมือนกับวัตถุอื่น ๆ วัตถุคณิตศาสตร์ไม่มีตัวสร้าง

วัตถุคณิตศาสตร์เป็นแบบคงที่

สามารถใช้วิธีการและคุณสมบัติทั้งหมดได้โดยไม่ต้องสร้างวัตถุคณิตศาสตร์ก่อน


คุณสมบัติทางคณิตศาสตร์ (ค่าคงที่)

ไวยากรณ์สำหรับคุณสมบัติ Math คือ:Math.property

JavaScript ให้ค่าคงที่ทางคณิตศาสตร์ 8 ค่าที่สามารถเข้าถึงได้เป็นคุณสมบัติทางคณิตศาสตร์:

ตัวอย่าง

Math.E        // returns Euler's number
Math.PI       // returns PI
Math.SQRT2    // returns the square root of 2
Math.SQRT1_2  // returns the square root of 1/2
Math.LN2      // returns the natural logarithm of 2
Math.LN10     // returns the natural logarithm of 10
Math.LOG2E    // returns base 2 logarithm of E
Math.LOG10E   // returns base 10 logarithm of E

วิธีคณิตศาสตร์

ไวยากรณ์สำหรับวิธีใด ๆ ทางคณิตศาสตร์คือ: Math.method(number)


ตัวเลขเป็นจำนวนเต็ม

มีวิธีทั่วไป 4 วิธีในการปัดเศษตัวเลขให้เป็นจำนวนเต็ม:

คณิตศาสตร์ รอบ(x)ส่งกลับค่า x ที่ปัดเศษเป็นจำนวนเต็มที่ใกล้ที่สุด
Math.ceil(x)ส่งกลับ x ที่ปัดเศษขึ้นเป็นจำนวนเต็มที่ใกล้เคียงที่สุด
คณิตศาสตร์ชั้น(x)ส่งกลับ x ที่ปัดเศษลงเป็นจำนวนเต็มที่ใกล้เคียงที่สุด
Math.trunc(x)ส่งกลับส่วนจำนวนเต็มของ x ( ใหม่ใน ES6 )

คณิตศาสตร์.รอบ()

Math.round(x) ส่งกลับจำนวนเต็มที่ใกล้เคียงที่สุด:

ตัวอย่าง

Math.round(4.6);
Math.round(4.5);
Math.round(4.4);

Math.ceil()

Math.ceil(x)ส่งคืนค่าของ x ที่ปัดเศษขึ้นเป็นจำนวนเต็มที่ใกล้เคียงที่สุด:

ตัวอย่าง

Math.ceil(4.9);
Math.ceil(4.7);
Math.ceil(4.4);
Math.ceil(4.2);
Math.ceil(-4.2);

คณิตศาสตร์ชั้น()

Math.floor(x)ส่งคืนค่าของ x ที่ปัดเศษลงเป็นจำนวนเต็มที่ใกล้เคียงที่สุด:

ตัวอย่าง

Math.floor(4.9);
Math.floor(4.7);
Math.floor(4.4);
Math.floor(4.2);
Math.floor(-4.2);

Math.trunc()

Math.trunc(x) ส่งคืนส่วนจำนวนเต็มของ x:

ตัวอย่าง

Math.trunc(4.9);
Math.trunc(4.7);
Math.trunc(4.4);
Math.trunc(4.2);
Math.trunc(-4.2);

Math.sign()

Math.sign(x)คืนค่าถ้า x เป็นค่าลบ, null หรือค่าบวก:

ตัวอย่าง

Math.sign(-4);
Math.sign(0);
Math.sign(4);

เพิ่ม Math.trunc() และ Math.sign() ใน JavaScript 2015 - ES6



Math.pow()

Math.pow(x, y)ส่งคืนค่า x ยกกำลัง y:

ตัวอย่าง

Math.pow(8, 2);

คณิตศาสตร์.sqrt()

Math.sqrt(x)คืนค่ารากที่สองของ x:

ตัวอย่าง

Math.sqrt(64);

Math.abs()

Math.abs(x)ส่งคืนค่าสัมบูรณ์ (บวก) ของ x:

ตัวอย่าง

Math.abs(-4.7);

คณิต.สิน ()

Math.sin(x)ส่งกลับค่าไซน์ (ค่าระหว่าง -1 ถึง 1) ของมุม x (กำหนดเป็นเรเดียน)

หากคุณต้องการใช้องศาแทนเรเดียน คุณต้องแปลงองศาเป็นเรเดียน:

มุมเป็นเรเดียน = มุมเป็นองศา x PI / 180

ตัวอย่าง

Math.sin(90 * Math.PI / 180);     // returns 1 (the sine of 90 degrees)

คณิตศาสตร์.cos()

Math.cos(x)ส่งกลับค่าโคไซน์ (ค่าระหว่าง -1 ถึง 1) ของมุม x (กำหนดเป็นเรเดียน)

หากคุณต้องการใช้องศาแทนเรเดียน คุณต้องแปลงองศาเป็นเรเดียน:

มุมเป็นเรเดียน = มุมเป็นองศา x PI / 180

ตัวอย่าง

Math.cos(0 * Math.PI / 180);     // returns 1 (the cos of 0 degrees)

Math.min() และ Math.max()

Math.min()และMath.max()สามารถใช้เพื่อค้นหาค่าต่ำสุดหรือสูงสุดในรายการอาร์กิวเมนต์:

ตัวอย่าง

Math.min(0, 150, 30, 20, -8, -200);

ตัวอย่าง

Math.max(0, 150, 30, 20, -8, -200);

คณิตศาสตร์สุ่ม()

Math.random()ส่งกลับตัวเลขสุ่มระหว่าง 0 (รวม) และ 1 (ไม่รวม):

ตัวอย่าง

Math.random();

คุณจะได้เรียนรู้เพิ่มเติมเกี่ยวกับMath.random()บทต่อไปของบทช่วยสอนนี้


Math.log() Method

Math.log(x)ส่งกลับค่าลอการิทึมธรรมชาติของ x

ลอการิทึมธรรมชาติส่งคืนเวลาที่จำเป็นเพื่อให้ถึงระดับการเติบโตที่แน่นอน:

ตัวอย่าง

Math.log(1);
Math.log(2);
Math.log(3);

Math.E และ Math.log() เป็นฝาแฝด

เราต้องคูณ Math.E กี่ครั้งเพื่อให้ได้ 10?

Math.log(10);

วิธี Math.log2()

Math.log2(x)ส่งกลับค่าลอการิทึมฐาน 2 ของ x

เราต้องคูณ 2 กี่ครั้งเพื่อให้ได้ 8?

Math.log2(8);

วิธี Math.log10()

Math.log10(x)ส่งกลับค่าลอการิทึมฐาน 10 ของ x

เราต้องคูณ 10 กี่ครั้งจึงจะได้ 1000?

Math.log10(1000);

วิธีการทางคณิตศาสตร์ของจาวาสคริปต์

Method Description
abs(x) Returns the absolute value of x
acos(x) Returns the arccosine of x, in radians
acosh(x) Returns the hyperbolic arccosine of x
asin(x) Returns the arcsine of x, in radians
asinh(x) Returns the hyperbolic arcsine of x
atan(x) Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians
atan2(y, x) Returns the arctangent of the quotient of its arguments
atanh(x) Returns the hyperbolic arctangent of x
cbrt(x) Returns the cubic root of x
ceil(x) Returns x, rounded upwards to the nearest integer
cos(x) Returns the cosine of x (x is in radians)
cosh(x) Returns the hyperbolic cosine of x
exp(x) Returns the value of Ex
floor(x) Returns x, rounded downwards to the nearest integer
log(x) Returns the natural logarithm (base E) of x
max(x, y, z, ..., n) Returns the number with the highest value
min(x, y, z, ..., n) Returns the number with the lowest value
pow(x, y) Returns the value of x to the power of y
random() Returns a random number between 0 and 1
round(x) Rounds x to the nearest integer
sign(x) Returns if x is negative, null or positive (-1, 0, 1)
sin(x) Returns the sine of x (x is in radians)
sinh(x) Returns the hyperbolic sine of x
sqrt(x) Returns the square root of x
tan(x) Returns the tangent of an angle
tanh(x) Returns the hyperbolic tangent of a number
trunc(x) Returns the integer part of a number (x)

อ้างอิงคณิตศาสตร์ที่สมบูรณ์

สำหรับข้อมูลอ้างอิงที่สมบูรณ์ ไปที่Complete Math Object Referenceของเรา

ข้อมูลอ้างอิงประกอบด้วยคำอธิบายและตัวอย่างของคุณสมบัติและวิธีการทางคณิตศาสตร์ทั้งหมด

ทดสอบตัวเองด้วยแบบฝึกหัด

ออกกำลังกาย:

ใช้วิธีการทางคณิตศาสตร์ที่ถูกต้องเพื่อสร้างตัวเลขสุ่ม

let r = ;