As we can see below, your example is actually spreading to 5 elements, where 2 of them are space characters. You can also see below that the spread operator on a string seems to be the same as using .split('').

const x = "1 2 3";
console.log([...x]);

console.log(x.split(''));
Answer from Olian04 on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Spread_syntax
Spread syntax (...) - JavaScript | MDN
The spread (...) syntax allows an iterable, such as an array or string, to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected. In an object literal, the spread syntax enumerates the properties of an object and adds the key-value ...
🌐
W3Schools
w3schools.com › react › react_es6_spread.asp
React ES6 Spread Operator
The JavaScript spread operator (...) allows us to quickly copy all or part of an existing array or object into another array or object.
Discussions

javascript - Spread operator for strings - Stack Overflow
I read about spread syntax on MDN and that it can be used with both arrays and strings: Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or More on stackoverflow.com
🌐 stackoverflow.com
arrays - How to loop over arguments passed via the spread operator in JavaScript? - Stack Overflow
2 How to pass a spread operator to a functions parameter · 1 How to pass array using spread syntax into method in JavaScript More on stackoverflow.com
🌐 stackoverflow.com
Understanding the Spread Operator in JavaScript
Spread Operators for Arrays The core piece to know is the ... syntax. This is the spread operator, and it essentially takes either an array or an object and expands it into its set of items. Careful here. Spreading into an array only works on objects if that object is iterable. [...{}] // Error, not iterable [...{ *[Symbol.iterator](){} }] // ok Array objects are inherently iterable so are ok, that is unless you break them :P const arr = [] arr[Symbol.iterator] = null [...arr] // Error, not iterable This does not apply to spreading in objects since that maps to Object.assign() which only cares about own enumerable properties. {...{}} // ok (doesn't have to be iterable) More on reddit.com
🌐 r/javascript
1
11
December 31, 2017
javascript - How does spread operator work in an array vs. obj? - Stack Overflow
I'm learning Redux from this tutorial and I don't get how the spread operator below works in both the object and array. If ...state returns the same thing, how can it work in both situations? I tho... More on stackoverflow.com
🌐 stackoverflow.com
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › advanced working with functions
Rest parameters and spread syntax
October 18, 2022 - The spread syntax internally uses iterators to gather elements, the same way as for..of does.
🌐
Appsmith
community.appsmith.com › content › guide › javascript-spread-operator-what-are-those-three-dots-my-code
The Javascript Spread Operator - What Are Those Three Dots in My Code? | Appsmith Community Portal
August 29, 2023 - In these examples, concat() is used to merge arrays, and Object.assign() is used to create a shallow copy of an object. While these alternatives work, they may require more lines of code compared to the concise spread operator.
🌐
SitePoint
sitepoint.com › blog › javascript › quick tip: how to use the spread operator in javascript
Quick Tip: How to Use the Spread Operator in JavaScript — SitePoint
November 7, 2024 - The JavaScript spread operator, symbolized by three dots (…), was introduced in ES6 and can be used to expand elements in collections and arrays into single, individual elements.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-spread-operator
JavaScript Spread Operator - GeeksforGeeks
The spread operator (...) with objects is used to create copies of existing objects with new or updated values or to make a copy of an object with more properties. Let's take an example of how to use the spread operator on an object,
Published   July 11, 2025
🌐
Programiz
programiz.com › javascript › spread-operator
JavaScript Spread Operator
The JavaScript spread operator (...) allows an iterable to be expanded in places where zero or more arguments or elements are expected. In this tutorial, you will learn about the JavaScript spread operator with the help of examples.
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_spread_operator.htm
JavaScript - Spread Operator
The JavaScript spread operator () allows us to spread out elements of an iterable such as an array. The spread operator is represented with three dots (). This is operator is introduced in ES6. The main use cases of the spread operator are to copy array elements, concatenate arrays or objects with r
🌐
DigitalOcean
digitalocean.com › community › tutorials › js-spread-operator
What's the Spread Operator Used For in JavaScript? | DigitalOcean
July 11, 2022 - The spread operator is a feature of JavaScript introduced with ES6 that gives you access to the insides of an iterable object. An “iterable object” is anything you can iterate over item by item, such as arrays, objects literals, and strings. These kinds of JavaScript types can be traversed ...
🌐
DEV Community
dev.to › marinamosti › understanding-the-spread-operator-in-javascript-485j
Understanding the Spread Operator in JavaScript - DEV Community
September 23, 2019 - Another great way to use the spread operator is to make a copy of an array or an object. In JavaScript when you assign an object or array to a variable you're actually storing something called the "pointer", which sort of works like the address of where that object is being stored.
Top answer
1 of 5
5

A spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments for functional calls are expected. Here is the MDN link

What the above means is that if you have a function that looks like the following

    function sum(x, y, z) { 
       return x + y + z;
    }

so the above function accepts three arguments, now when you pass in an array of length 3 using the spread operator, it replaces the values x, y, z with numbers[0], numbers1, numbers[2] respectively.

So you would be calling the above function such as

console.log(sum(...numbers));

function sum(x, y, z) {
  return x + y + z;
}

const numbers = [10, 12, 13];

console.log(sum(...numbers)); //output = 35

As mentioned in the comment to explain the code that you have in place, the ...numbers in the function sum means, there is no defined number of arguments, instead accept as many as passed. Now you are calling the function sum with a single argument of type array, so this is interpreted as the first parameter to the function sum to be of type array and not what you expect/intend.

Now In order to fix or achieve what you intend to do you got to spread the array that you are passing in as argument along the lines of

function sum(...numbers) {  
  let sum = 0;
  for (i = 0; i < numbers.length; i++) {
    sum += numbers[i];
  }
  return sum;
}

function returnsAnArray() {
  return [1,2,3,4];
}

console.log(sum(...returnsAnArray()));
2 of 5
3

If the argument is a singular array then it the array is provided as is.

No, you will get an array with an array. You have to spread the array to get what you expect:

 sum(...returnsAnArray())
🌐
Reddit
reddit.com › r/javascript › understanding the spread operator in javascript
r/javascript on Reddit: Understanding the Spread Operator in JavaScript
December 31, 2017 - syntax. This is the spread operator, and it essentially takes either an array or an object and expands it into its set of items.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Destructuring
Destructuring - JavaScript | MDN
The destructuring uses similar syntax but uses it on the left-hand side of the assignment instead. It performs the reverse operation of an array declaration, by declaring each element in the collection as a separate variable.
🌐
Medium
medium.com › @anton.martyniuk › spread-and-rest-operators-in-javascript-a5d1f1ee60dd
Spread and Rest Operators in JavaScript | by Anton Martyniuk | Medium
March 21, 2024 - While the spread operator expands an array or object into its individual elements, the rest operator does the opposite, collecting multiple elements or properties into a single array or object.
🌐
CoreUI
coreui.io › answers › how-to-use-spread-operator-in-javascript
How to use spread operator in JavaScript · CoreUI
1 month ago - Here ...arr1 and ...arr2 expand ... 3, d: 4 }. The spread operator creates shallow copies, making it perfect for immutable updates, function argument passing, and avoiding array/object mutation....
Top answer
1 of 3
13

UPDATE

Spread syntax allows you to spread an array into an object (arrays are technically objects, as is mostly everything in js). When you spread an array into an object, it will add a key: value pair to the object for each array item, where the key is the index and the value is the value stored at that index in the array. For example:

const arr = [1,2,3,4,5]
const obj = { ...arr } // { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5 }

const arr2 = [{ name: 'x' }, { name: 'y' }]
const obj2 = { ...arr2 } // { 0: { name: 'x' }, 1: { name: 'y' } }

You can also spread strings into arrays and objects as well. For arrays, it will behave similarly as String.prototype.split:

const txt = 'abcdefg'
const arr = [...txt] // ['a','b','c','d','e','f', 'g']

For objects, it will split the string by character and assign keys by index:

const obj = { ...txt } // { 0:'a',1:'b',2:'c',3:'d',4:'e',5:'f',6:'g' }

So you may be getting data that sort of works when you spread an array into an object. However, if the example you gave is what you're actually using, you're going to run into problems. See below.

=============

In the case of reducers in redux, when you use the spread syntax with an array it spreads each item from your array into a new array. It's basically the same as using concat:

const arr = [1,2,3]
const arr2 = [4,5,6]
const arr3 = [...arr, ...arr2] // [1,2,3,4,5,6]
// same as arr.concat(arr2)

With an object, the spread syntax spreads key: value pairs from one object into another:

const obj = { a: 1, b: 2, c: 3 }
const newObj = { ...obj, x: 4, y: 5, z: 6 }
// { a: 1, b: 2, c: 3, x: 4, y: 5, z: 6 }

These are two ways to help keep your data immutable in your reducers. The spread syntax copies array items or object keys/values rather than referencing them. If you do any changes in nested objects or objects in arrays, you'll have to take that into account to make sure you get new copies instead of mutated data.

If you have arrays as object keys then you can spread the entire object into a new one and then override individual keys as needed, including keys that are arrays that need updating with spread syntax. For example, an update to your example code:

const initialState = {
  images: [],
  videos: [],
  selectedVideo: ''
}

// you need all of your initialState here, not just one of the keys
export default function ( state = initialState, action ) {
  switch (action.type) {
    case types.SELECTED_VIDEO:
      // spread all the existing data into your new state, replacing only the selectedVideo key
      return {
        ...state,
        selectedVideo: action.video
      }
    case types.SHUTTER_VIDEO_SUCCESS:
      // spread current state into new state, replacing videos with the current state videos and the action videos
      return {
        ...state,
        videos: [...state.videos, ...action.videos]
      }
    default:
      return state;
  }
}

This shows updating a state object and specific keys of that object that are arrays.

In the example you give, you're changing the structure of your state on the fly. It starts as an array, then sometimes returns an array (when SHUTTER_VIDEO_SUCCESS) and sometimes returns an object (when SELECTED_VIDEO). If you want to have a single reducer function, you would not isolate your initialState to just the videos array. You would need to manage all of your state tree manually as shown above. But your reducer should probably not switch the type of data it's sending back depending on an action. That would be an unpredictable mess.

If you want to break each key into a separate reducer, you would have 3 (images, videos and selectedVideo) and use combineReducers to create your state object.

import { combineReducers } from 'redux'
// import your separate reducer functions

export default combineReucers({
  images,
  videos,
  selectedVideos
})

In that case each reducer will be run whenever you dispatch an action to generate the complete state object. But each reducer will only deal with its specific key, not the whole state object. So you would only need array update logic for keys that are arrays, etc.

2 of 3
0

According to the tutorial:

create-react-app comes preinstalled with babel-plugin-transform-object-rest-spread that lets you use the spread (…) operator to copy enumerable properties from one object to another in a succinct way. For context, { …state, videos: action.videos } evaluates to Object.assign({}, state, action.videos).

So, that's not a feature of ES6. It uses a plugin to let you use that feature.

Link: https://babeljs.io/docs/plugins/transform-object-rest-spread/

🌐
W3Schools
w3schools.com › howto › howto_js_spread_operator.asp
How To Use the Spread Operator (...) in JavaScript
The JavaScript spread operator (...) expands an iterable (like an array) into more elements.
🌐
Medium
medium.com › @slamflipstrom › conditional-object-properties-using-spread-in-javascript-714e0a12f496
Conditional Object Properties Using Spread in JavaScript | by Sam Lindstrom | Medium
June 6, 2019 - While recently browsing Stack Overflow, ... ES6’s spread syntax. The spread operator ( ... ) allows us to expand iterables into function calls, array literals, and object literals — if you’re using ES2018....