Hàm có thể nhận nhiều tham số, phân cách bằng dấu phẩy. Bên trong hàm truy cập qua đối tượng arguments hoặc tham số trực tiếp.
Ví dụ
<!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>Tham số hàm trong JavaScript</h1> <div class="sample"></div> <button class="Btn">NHẤN VÀO ĐÂY</button> <h3>Nhấn nút để xem tham số hàm</h3> <script> let sampleEle = document.querySelector(".sample"); function test(param1, param2, param3, param4) { sampleEle.innerHTML = "Các tham số của hàm: <br>"; Array.from(arguments).forEach((element, index) => { sampleEle.innerHTML += "Tham số " + index + " = " + element + "<br>"; }); } document.querySelector(".Btn").addEventListener("click", () => { test("Hello", "world", 22, 99); }); </script> </body> </html>
Kết quả
Hiển thị 4 tham số: Hello, world, 22, 99.