Spread Operator in JavaScript
You can use the … spread operator to pass the elements of an array or other iterable object as arguments in a function invocation. The ...
spread operator can be used to spread out the elements of an iterable, like an array, as arguments in a function call. Here is an example of how you can use it:
|
|
In this example, the array numbers
is spread out into its individual elements (1, 2, 3
) by the ...
operator, and these are then passed as arguments to the sum
function. This is equivalent to calling sum(1, 2, 3)
.
The spread operator can be particularly useful when you have an array of values that you want to pass to a function that expects separate arguments, or when you’re working with a function that can take any number of arguments. It essentially allows you to treat an array as a list of function arguments, which can be very convenient.