ส่งพารามิเตอร์ที่แตกต่างกันขณะเรียกใช้ฟังก์ชัน พารามิเตอร์ที่ส่งเข้ามานี้สามารถจับได้ภายในฟังก์ชันและสามารถทำการจัดการใดๆ กับพารามิเตอร์เหล่านั้นได้ ฟังก์ชันสามารถรับพารามิเตอร์หลายตัว คั่นด้วยเครื่องหมายจุลภาค
โค้ดตัวอย่างสำหรับการนำพารามิเตอร์ของฟังก์ชันไปใช้ใน JavaScript ดังนี้ -
ตัวอย่าง
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .sample { font-size: 18px; font-weight: 500; } </style> </head> <body> <h1>JavaScript Function Parameters</h1> <div class="sample"></div> <button class="Btn">CLICK HERE</button> <h3>คลิกปุ่มข้างบนเพื่อดูพารามิเตอร์ของฟังก์ชัน</h3> <script> let sampleEle = document.querySelector(".sample"); test(param1, param2, param3, param4) { sampleEle.innerHTML = "The parameters for the function are <br>"; Array.from(arguments).forEach((element, index) => { sampleEle.innerHTML += "Parameter " + index + " = " + element + "<br>"; }); } document.querySelector(".Btn").addEventListener("click", () => { test("Hello", "world", 22, 99); }); </script> </body> </html>
ผลลัพธ์

เมื่อคลิกปุ่ม "CLICK HERE" -
