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.

Answer from shadymoses 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.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-spread-operator
JavaScript Spread Operator - GeeksforGeeks
The Spread operator (represented as three dots or …) is used on iterables like array and string, or properties of Objects, to expand wherever zero or more elements are required to be copied or assigned.
Published   July 11, 2025
🌐
SamanthaMing
samanthaming.com › tidbits › 92-6-use-cases-of-spread-with-array
6 Use Case of Spread with Array in JavaScript | SamanthaMing.com
MDN: Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places ...
🌐
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.
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/

🌐
freeCodeCamp
freecodecamp.org › news › javascript-spread-and-rest-operators
JavaScript Spread and Rest Operators – Explained with Code Examples
February 8, 2024 - You can get all the source code from here. ... The spread operator, denoted by three consecutive dots (...), is primarily used for expanding iterables like arrays into individual elements.
🌐
DEV Community
dev.to › marinamosti › understanding-the-spread-operator-in-javascript-485j
Understanding the Spread Operator in JavaScript - DEV Community
September 23, 2019 - Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places ...
Find elsewhere
🌐
Programiz
programiz.com › javascript › spread-operator
JavaScript Spread Operator
The JavaScript spread operator ... is used to expand or spread out elements of an iterable, such as an array, string, or object.
🌐
Kinsta®
kinsta.com › home › resource center › knowledge base › javascript tutorials › unleashing the power of javascript spread operator
Unleashing the Power of JavaScript Spread Operator - Kinsta®
July 7, 2023 - The spread operator in JavaScript is a syntax introduced in ECMAScript 6 (ES6) that allows you to spread the elements of an iterable (such as arrays, strings, or objects), into another iterable or function call.
🌐
GPTWebHelper
blixtdev.com › how-to-use-the-spread-operator-in-javascript
How to Use The Spread Operator in JavaScript
December 20, 2022 - Simply put, the spread operator in JavaScript expands or unpacks the elements of an array or iterable object.
🌐
Medium
cleverzone.medium.com › demystifying-javascript-spread-operat-71924c888a8
Demystifying JavaScript : Spread operator (…) | by Cleverzone | Medium
August 26, 2023 - The spread operator, denoted by three dots (...), allows JavaScript developers to easily split array elements or object properties and spread them into a new array or object. This operator is aptly named "spread" because it enables us to spread ...
🌐
Dillion's Blog
dillionmegida.com › p › spread-operator-simplified
Spread Operator in JavaScript, Simplified - Dillion's Blog
The spread operator is used to unroll (to "spread", like butter on bread 😂) the individual elements of an iterable object or array (iterable collection), separated by a comma, into another collection.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript array methods › javascript spread operator
The Practical Usages of JavaScript Spread Operator
May 8, 2024 - ES6 provides a new operator called spread operator that consists of three dots (...). The spread operator allows you to spread out elements of an iterable object such as an array...
🌐
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.
🌐
Educative
educative.io › answers › what-is-the-spread-operator-in-javascript
What is the spread operator in JavaScript?
Instead of having to pass each element like numbers[0], numbers[1] and so on, the spread operators allows array elements to be passed in as individual arguments. ... The Math object of Javascript does not take in a single array as an argument ...
🌐
Medium
medium.com › coding-at-dawn › how-to-use-the-spread-operator-in-javascript-b9e4a8b06fab
How to use the spread operator (…) in JavaScript
November 19, 2020 - “When ...arr is used in the function call, it ‘expands’ an iterable object arr into the list of arguments.” — JavaScript.info · The spread operator was added to JavaScript in ES6 (ES2015), just like the rest parameters, which have the same syntax: three magic dots ….
🌐
Playcode
playcode.io › javascript › spread-operator
Spread Operator
Try this online JavaScript Playground with instant live preview and console. Easy & Fast. Experiment yourself.
🌐
Stack Abuse
stackabuse.com › spread-operator-in-javascript
Spread Operator in JavaScript
August 24, 2023 - In this tutorial, we'll demystify ... statement. We can use the spread operator on iterables like a String or an array and it'll put the contents of the iterable into individual elements....
🌐
SheCodes
shecodes.io › athena › 101463-what-is-spread-operator-and-how-to-use-it-in-practice
[JavaScript] - What is spread operator and how to use it in | SheCodes
Learn about the spread operator in JavaScript and how to use it for array concatenation, function arguments, and object merging.