Spread operator for arrays in JavaScript

The spread (…) syntax allow us to expand an iterable like array in places where 0+ arguments are expected. It allows us to pass a number of parameters as an array to a function.

Following is the code to implement spread operator for arrays in JavaScript −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   .sample {
      font-size: 20px;
      font-weight: 500;
      color: blueviolet;
   .sample {
      color: red;
<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>
   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);


On clicking the ‘CLICK HERE’ button −

Updated on: 18-Jul-2020


Kickstart Your Career

Get certified by completing the course

Get Started