วัตถุ JavaScript Math ช่วยให้คุณสามารถทำงานทางคณิตศาสตร์ได้ ตัวเลข
Math.PI;
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.PI</h2>
<p>Math.PI returns the ratio of a circle's circumference to its diameter:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.PI;
</script>
</body>
</html>
วัตถุ 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
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math Constants</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"<p><b>Math.E:</b> " + Math.E + "</p>" +
"<p><b>Math.PI:</b> " + Math.PI + "</p>" +
"<p><b>Math.SQRT2:</b> " + Math.SQRT2 + "</p>" +
"<p><b>Math.SQRT1_2:</b> " + Math.SQRT1_2 + "</p>" +
"<p><b>Math.LN2:</b> " + Math.LN2 + "</p>" +
"<p><b>Math.LN10:</b> " + Math.LN10 + "</p>" +
"<p><b>Math.LOG2E:</b> " + Math.LOG2E + "</p>" +
"<p><b>Math.Log10E:</b> " + Math.LOG10E + "</p>";
</script>
</body>
</html>
ไวยากรณ์สำหรับวิธีการทางคณิตศาสตร์ใดๆ คือ : Math.method(number)
มี 4 วิธีทั่วไปในการปัดเศษตัวเลขเป็นจำนวนเต็ม:
ส่งคืนค่า x ปัดเศษเป็นจำนวนเต็มที่ใกล้ที่สุด
ส่งคืนค่า x ปัดเศษขึ้นเป็นจำนวนเต็มที่ใกล้ที่สุด
ส่งคืนค่า x ปัดเศษลงให้เป็นจำนวนเต็มที่ใกล้ที่สุด
ส่งกลับส่วนจำนวนเต็มของ x (ใหม่ใน ES6)
Math.round()
Math.round(x)
ส่งกลับจำนวนเต็มที่ใกล้ที่สุด:
Math.round(4.6);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.round()</h2>
<p>Math.round(x) returns the value of x rounded to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.round(4.6);
</script>
</body>
</html>
Math.round(4.5);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.round()</h2>
<p>Math.round(x) returns the value of x rounded to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.round(4.5);
</script>
</body>
</html>
Math.round(4.4);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.round()</h2>
<p>Math.round(x) returns the value of x rounded to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.round(4.4);
</script>
</body>
</html>
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);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.ceil()</h2>
<p>Math.ceil() rounds a number <strong>up</strong> to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.ceil(4.4);
</script>
</body>
</html>
Math.floor()
Math.floor(x)
ส่งกลับค่า x ปัดเศษ ลง ให้เป็นจำนวนเต็มที่ใกล้ที่สุด:
Math.floor(4.9);
Math.floor(4.7);
Math.floor(4.4);
Math.floor(4.2);
Math.floor(-4.2);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.floor()</h2>
<p>Math.floor(x) returns the value of x rounded <strong>down</strong> to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.floor(4.7);
</script>
</body>
</html>
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);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.trunc()</h2>
<p>Math.trunc(x) returns the integer part of x:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.trunc(4.7);
</script>
</body>
</html>
Math.sign()
Math.sign(x)
ส่งคืนหาก x เป็นค่าลบ ค่าว่าง หรือค่าบวก:
Math.sign(-4);
Math.sign(0);
Math.sign(4);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.sign()</h2>
<p>Math.sign(x) returns if x is negative, null or positive:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.sign(4);
</script>
</body>
</html>
Math.trunc() และ Math.sign() ถูกเพิ่มใน JavaScript 2015 - ES6
Math.pow()
Math.pow(x, y)
ส่งกลับค่าของ x ยกกำลังของ y:
Math.pow(8, 2);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.pow()</h2>
<p>Math.pow(x,y) returns the value of x to the power of y:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.pow(8,2);
</script>
</body>
</html>
Math.sqrt()
Math.sqrt(x)
คืนค่ารากที่สองของ x:
Math.sqrt(64);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.sqrt()</h2>
<p>Math.sqrt(x) returns the square root of x:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.sqrt(64);
</script>
</body>
</html>
Math.abs()
Math.abs(x)
ส่งกลับค่าสัมบูรณ์ (บวก) ของ x:
Math.abs(-4.7);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.abs()</h2>
<p>Math.abs(x) returns the absolute (positive) value of x:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.abs(-4.7);
</script>
</body>
</html>
Math.sin()
Math.sin(x)
ส่งคืนไซน์ (ค่าระหว่าง -1 ถึง 1) ของมุม x (กำหนดเป็นเรเดียน)
หากคุณต้องการใช้องศาแทนเรเดียน คุณต้องแปลงองศาเป็นเรเดียน:
มุมเป็นเรเดียน=มุมเป็นองศา x PI/180
Math.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees)
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.sin()</h2>
<p>Math.sin(x) returns the sin of x (given in radians):</p>
<p>Angle in radians = (angle in degrees) * PI / 180.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"The sine value of 90 degrees is " + Math.sin(90 * Math.PI / 180);
</script>
</body>
</html>
Math.cos()
Math.cos(x)
ส่งคืนโคไซน์ (ค่าระหว่าง -1 ถึง 1) ของมุม x (กำหนดเป็นเรเดียน)
หากคุณต้องการใช้องศาแทนเรเดียน คุณต้องแปลงองศาเป็นเรเดียน:
มุมเป็นเรเดียน=มุมเป็นองศา x PI/180
Math.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees)
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.cos()</h2>
<p>Math.cos(x) returns the cosine of x (given in radians):</p>
<p>Angle in radians = (angle in degrees) * PI / 180.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"The cosine value of 0 degrees is " + Math.cos(0 * Math.PI / 180);
</script>
</body>
</html>
Math.min() / Math.max()
Math.min()
และ Math.max()
สามารถใช้ค้นหาค่าต่ำสุดหรือสูงสุดใน รายการข้อโต้แย้ง:
Math.min(0, 150, 30, 20, -8, -200);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.min()</h2>
<p>Math.min() returns the lowest value in a list of arguments:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
Math.min(0, 150, 30, 20, -8, -200);
</script>
</body>
</html>
Math.max(0, 150, 30, 20, -8, -200);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.max()</h2>
<p>Math.max() returns the highest value in a list of arguments.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
Math.max(0, 150, 30, 20, -8, -200);
</script>
</body>
</html>
Math.random()
Math.random()
ส่งกลับตัวเลขสุ่มระหว่าง 0 (รวม) และ 1 (พิเศษ):
Math.random();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.random()</h2>
<p>Math.random() returns a random number between 0 and 1:</p>
<p id="demo"></p>
<p>Tip: Click on "Run" several times.</p>
<script>
document.getElementById("demo").innerHTML = Math.random();
</script>
</body>
</html>
คุณจะได้เรียนรู้เพิ่มเติมเกี่ยวกับ Math.random()
ในบทถัดไปของบทช่วยสอนนี้
Math.log(x)
ส่งคืนลอการิทึมธรรมชาติของ x
ลอการิทึมธรรมชาติส่งคืนเวลาที่จำเป็นในการไปถึงระดับการเติบโตหนึ่ง:
Math.log(1);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log()</h2>
<p>Math.log() returns the natural logarithm of a number:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log(1);
</script>
</body>
</html>
Math.log(2);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log()</h2>
<p>Math.log() returns the natural logarithm of a number:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log(2);
</script>
</body>
</html>
Math.log(3);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log()</h2>
<p>Math.log() returns the natural logarithm of a number:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log(3);
</script>
</body>
</html>
Math.E และ Math.log() เป็นฝาแฝดกัน
เราต้องคูณ Math.E กี่ครั้งจึงจะได้ 10?
Math.log(10);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log()</h2>
<p>How many times must we multiply Math.E to get 10?</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log(10);
</script>
</body>
</html>
Math.log2(x)
ส่งกลับค่าลอการิทึมฐาน 2 ของ x
เราต้องคูณ 2 กี่ครั้งจึงจะได้ 8?
Math.log2(8);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log2()</h2>
<p>Math.log2() returns the base 2 logarithm of a number.</p>
<p>How many times must we multiply 2 to get 8?</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log2(8);
</script>
</body>
</html>
Math.log10(x)
ส่งกลับค่าลอการิทึมฐาน 10 ของ x
เราต้องคูณ 10 กี่ครั้งจึงจะได้ 1,000?
Math.log10(1000);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log10()</h2>
<p>Math.log10() returns the base 10 logarithm of a number.</p>
<p>How many times must we multiply 10 to get 1000?</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log10(1000);
</script>
</body>
</html>
ส่งกลับค่าสัมบูรณ์ของ x
ส่งกลับค่าอาร์กโคไซน์ของ x ในหน่วยเรเดียน
ส่งกลับค่าไฮเปอร์โบลิกอาร์คโคไซน์ของ x
ส่งกลับค่าอาร์คไซน์ของ x ในหน่วยเรเดียน
ส่งกลับค่าไฮเปอร์โบลิกอาร์กไซน์ของ x
ส่งกลับค่าอาร์กแทนเจนต์ของ x เป็นค่าตัวเลขระหว่าง -PI/2 ถึง PI/2 เรเดียน
ส่งกลับค่าอาร์กแทนเจนต์ของผลหารของอาร์กิวเมนต์
ส่งกลับค่าไฮเปอร์โบลิกอาร์กแทนเจนต์ของ x
ส่งกลับรากลูกบาศก์ของ x
ส่งกลับค่า x โดยปัดเศษขึ้นให้เป็นจำนวนเต็มที่ใกล้ที่สุด
ส่งกลับค่าโคไซน์ของ x (x อยู่ในหน่วยเรเดียน)
ส่งกลับค่าไฮเปอร์โบลิกโคไซน์ของ x
ส่งกลับค่าของ Ex
ส่งกลับค่า x โดยปัดเศษลงให้เป็นจำนวนเต็มที่ใกล้ที่สุด
ส่งกลับค่าลอการิทึมธรรมชาติ (ฐาน E) ของ x
ส่งกลับตัวเลขที่มีค่าสูงสุด
ส่งกลับตัวเลขที่มีค่าต่ำสุด
ส่งกลับค่า x ยกกำลัง y
ส่งกลับตัวเลขสุ่มระหว่าง 0 ถึง 1
ปัดเศษ x เป็นจำนวนเต็มที่ใกล้ที่สุด
ส่งกลับถ้า x เป็นค่าลบ ค่าว่าง หรือค่าบวก (-1, 0, 1)
ส่งกลับค่าไซน์ของ x (x อยู่ในหน่วยเรเดียน)
ส่งกลับค่าไฮเปอร์โบลิกไซน์ของ x
ส่งคืนค่ารากที่สองของ x
ส่งกลับค่าแทนเจนต์ของมุม
ส่งกลับค่าไฮเปอร์โบลิกแทนเจนต์ของตัวเลข
ส่งกลับส่วนจำนวนเต็มของตัวเลข (x)
หากต้องการข้อมูลอ้างอิงฉบับเต็ม โปรดไปที่การอ้างอิงวัตถุทางคณิตศาสตร์ฉบับสมบูรณ์ของเรา
ข้อมูลอ้างอิงประกอบด้วยคำอธิบายและตัวอย่างคุณสมบัติและวิธีการทางคณิตศาสตร์ทั้งหมด