JS-101-Fundamentals
  • JavaScript Library
  • 1-JavaScript Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-JS-Fundamentals
      • 1-Grammar and Types
        • Comments
        • Declarations
        • Scope
        • Hoisting
        • Types
        • Literals
      • 2-Control Flow and Error Handling
        • if
        • if else
        • switch
        • try catch
        • throw
      • 3-Loops
        • For Loops
        • For In Loops
        • For Of Loops
        • Do While
        • While Loops
      • 4-Functions
        • Declarations
        • Expressions
        • Calling Functions
        • Scope
        • Parameters
        • Arguments
        • Closures
        • Closures Challenge
        • Arrow Functions
        • Predefined
      • 5-Expressions and Operators
        • Assignment
        • Comparison
        • Ternary
        • Typeof
        • Left Hand Side
        • Spread
      • 6 Numbers and Dates
        • Numbers
          • Numbers Enhanced
        • Math
        • Dates
      • 7 String Methods
        • String Literals
        • Methods
      • 8 Regular Expressions
        • Basic Intro
      • 9 Arrays
        • Array Review
        • Populating/Referring
        • Length
        • Iterating
        • Methods
      • 10 Objects
        • About Objects
        • Properties
        • Enumeration
        • Initializers
        • Constructor Functions
        • this
        • create
        • Methods
      • 11 ES6 Intro
        • ES6 Intro
        • let
        • const
Powered by GitBook
On this page
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 5-Expressions and Operators

Spread

The spread operator was introduced with ES6, and is represented by an ellipse ( ... ). Spread allows for multiple items to be into an array or a function. We will cover the array portion in more depth later, so for now we'll focus on the function uses

spread syntax

Essentially, the spread operator allows us to place multiple items in a single variable, then insert them directly into the function as parameters. Complex functions can have many parameters, and as the code grows, being able to place those parameters in a variable rather than type them out each time is useful in cutting down on extra code. For example, consider the function for creating a Date object with a specific date and time:

new Date(year, month [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);

This function can contain up to a total of 7 parameters. Imagine having to use that function over and over and over, writting it every single time. Using the spread operator, we can put all the parameters into an array and inject it straight into the function:

let params = [2018, 2, 28, 16, 30, 0, 0];
let date = new Date(...params);

In the event that we need to create this Date object multiple times, using ...params will significantly cut down on extra code and refactoring time. Later you'll learn other uses of the spread operator, including the ability inject items directly into a specific index of an array.

PreviousLeft Hand SideNext6 Numbers and Dates

Last updated 7 years ago