확산(…) 구문을 사용하면 0개 이상의 인수가 예상되는 위치에서 배열과 같은 반복 가능을 확장할 수 있습니다. 여러 매개변수를 배열로 함수에 전달할 수 있습니다.
다음은 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;
}
.result,
.sample {
font-size: 20px;
font-weight: 500;
color: blueviolet;
}
.sample {
color: red;
}
</style>
</head>
<body>
<h1>Spread operator for arrays JavaScript</h1>
<div class="sample">[22,55,11,19,55]</div>
<div class="result"></div>
<br />
<button class="Btn">CLICK HERE</button>
<h3>Click on the above button to call the add function and pass the above
array as parameter</h3>
<script>
let BtnEle = document.querySelector(".Btn");
let resEle = document.querySelector(".result");
function add(num1, num2, num3, num4, num5) {
return num1 + num2 + num3 + num4 + num5;
}
let arr = [22, 55, 11, 19, 55];
BtnEle.addEventListener("click", (event) => {
resEle.innerHTML = "The sum of the above array = " + add(...arr);
});
</script>
</body>
</html> 출력

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