วิธีการ Array.of() ของ JavaScript ใช้เพื่อสร้างอินสแตนซ์อาร์เรย์ใหม่ที่มีตัวแปรเป็นค่าพารามิเตอร์
ไวยากรณ์มีดังนี้ −
Array.of(elements....)
ด้านบน องค์ประกอบคือค่าที่เป็นค่าพารามิเตอร์
ให้เราใช้วิธี Array.of() ใน JavaScript -
ตัวอย่าง
<!DOCTYPE html>
<html>
<body>
<h2>Demo Heading</h2>
<p>Click the button to display the values in Console</p>
<button onclick="display()">Result</button>
<p id="test"></p>
<script>
function display() {
console.log(Array.of(10, 20, 30, 40 ,50));
}
</script>
</body>
</html> ผลลัพธ์

คลิก "ผลลัพธ์" และตรวจสอบคอนโซลสำหรับผลลัพธ์ -

ตัวอย่าง
<!DOCTYPE html>
<html>
<body>
<h2>Demo Heading</h2>
<p>Get the student names with top 5 ranks...</p>
<button onclick="display()">Result</button>
<p id="test"></p>
<script>
function display() {
console.log(Array.of("Kevin", "Tom", "Ryan", "Jacob", "Jack"));
}
</script>
</body>
</html> ผลลัพธ์

คลิกปุ่ม "ผลลัพธ์" และตรวจสอบผลลัพธ์ในคอนโซล -
