ES6+ for React
  • Introduction
  • Introduction
    • Introduction
  • Statements and declarations
    • let
    • const
    • class
    • import
    • export
  • Standard built-in objects
    • Arrow functions
    • Template literals
    • Array.prototype.map()
    • Computed property names
  • Expressions and operators
    • Spread operator
    • Destructuring assignment
    • Logical Operators
Powered by GitBook
On this page
  • Defination
  • Syntax
  • Examples
  • Usage
  • References

Was this helpful?

  1. Expressions and operators

Spread operator

Defination

The spread syntax allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) or multiple variables (for destructuring assignment) are expected.

Syntax

// For function calls:
myFunction(...iterableObj);
// For array literals:
[...iterableObj, 4, 5, 6];
// For object literals (new in ECMAScript; stage 3 draft):
let objClone = { ...obj };

Examples

// Function
function myFunction(x, y, z) { }
var args = [0, 1, 2];
myFunction(...args);
// Array
var parts = ['shoulders', 'knees']; 
var lyrics = ['head', ...parts, 'and', 'toes']; 
// ["head", "shoulders", "knees", "and", "toes"]
// Object
var obj1 = { foo: 'bar', x: 42 };
var obj2 = { foo: 'baz', y: 13 };

var clonedObj = { ...obj1 };
// Object { foo: "bar", x: 42 }

var mergedObj = { ...obj1, ...obj2 };
// Object { foo: "baz", x: 42, y: 13 }

Usage

function todos(state = [], action) {
  switch (action.type) {
    case ADD_TODO:
      return [
        ...state,
        {
          text: action.text,
          completed: false
        }
      ]
    case TOGGLE_TODO:
      return state.map((todo, index) => {
        if (index === action.index) {
          return Object.assign({}, todo, {
            completed: !todo.completed
          })
        }
        return todo
      })
    default:
      return state
  }
}

References

PreviousComputed property namesNextDestructuring assignment

Last updated 4 years ago

Was this helpful?

MDN Spread operator