Use of spread operator


I’ve been reading Redux documentation and I saw this piece of code, which returns state using spread operator. I don’t understand its use in this context. What exactly does it do? What does ...state mean (is)?

function todos(state = [], action) {
  switch (action.type) {
    case 'ADD_TODO':
      return [
          text: action.text,
          completed: false
    case 'COMPLETE_TODO':
      return [
        ...state.slice(0, action.index),
        Object.assign({}, state[action.index], {
          completed: true
        ...state.slice(action.index + 1)
      return state


...state is a copy of state, rather than a reference to it.

I’m unclear as to why ...state.slice(), though, since slice also creates a new array. Perhaps just for consistency (vs performance tradeoff).


So, what’s the difference between returning ...state in case of ADD_TODOS and the default one when ‘just’ state is returned?


In general, to prevent mutations of state from mutating the result of the function. In this specific case, I don’t know.


Thanks for your help :grinning:!