함수를 호출하는 동안 다른 매개변수를 전달합니다. 이러한 전달된 매개변수는 함수 내부에서 캡처할 수 있으며 해당 매개변수에 대해 모든 조작을 수행할 수 있습니다. 함수는 쉼표로 구분된 여러 매개변수를 사용할 수 있습니다.
다음은 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>Click on the above buttons to see the function parameters </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> 출력

"여기를 클릭" 버튼을 클릭하면 -
