by Sai gowtham

How to use Rest Parameters (...) in JavaScript

Rest Parameters ( … )

Rest parameters help us to represent the infinite number of function arguments as an array.

function sum(a,b,...remaining){






//a -> 1

//b - > 2

//remaining - > [3,4,5,6,7]

In the above code sum function accepts three arguments a, b and remaining. where the first two arguments values are 1,2 everything we passed after the first two arguments are represented as an array.

function sortNumbers(...numbers){

    return numbers.sort((a,b)=>a-b)



// [1,2,3,4,5,6,7]

If we prefix any parameter with (…) that parameter can accept unlimited arguments and represent it as an array. Like in the above example we prefixed ...numbers so that it can accept many arguments.

Happy coding…