Skip to content
Advertisement

Why spread operator converts object param to one item array?

JavaScript

JavaScript

So in the above code, obj1 is an object. So I use spread operator in the function definition, and give it an object when invoking the function. why the result is an array with one item of that input object? What is the syntax here? I didn’t find any explanation in MDN about spread operator. Please help me explain.

Advertisement

Answer

This is not spread. These are rest parameters, which collect all remaining arguments passed into a single array.

Here, there’s one argument, so using rest creates an array containing just that one argument (which happens to be an object).

Here are a couple other examples that might make it clearer:

JavaScript

It’ll just log all arguments passed, in the form of a single array.

Advertisement