> For the complete documentation index, see [llms.txt](https://ga0-2.gitbook.io/seifxr10anz-content/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ga0-2.gitbook.io/seifxr10anz-content/week-13/day-3-destructuring-spread-rest-and-ajax/spread-and-rest.md).

# Spread & Rest

#### Spread

The spread operator allows us to expand the elements of an array or the properties of an object into another array or object, respectively.

```javascript
const countries = ["au", "nz", "sg", "jp", "us"];
// console.log(countries[0], countries[1], countries[2], countries[3], countries[4])
console.log(...countries)

// can be used to create a shallow copy of the original array
const countriesCopy = [...countries]

const scores = [29, 24, 28, 92, 1]
const moreScores = [2, ...scores, 32, 88]
console.log(moreScores)
```

```javascript
const john = { name: "john", location: "iceland", year: 2003 }

// This operator can be used for creating a shallow copy of the original object.
// const cloneOfJohn = Object.assign({}, john)
const cloneOfJohn = { ...john }

// Combine two objects
const programmer = { experience: 5, language: "snake python" }
const johnTheProgrammer = { ...john, ...programmer }
```

#### Rest

In function definitions, the rest operator allows us to accept an unknown number of arguments, which are stored in an array.

```javascript
const printMyFavoriteColours = (...colours) => {
  console.log(`My favorite colours are:`)
  for (let colour of colours) {
    console.log(colour)
  }
}
printMyFavoriteColours("red", "blue")
printMyFavoriteColours("white", "grey", "yellow")
```

In destructuring, it allows us to represent the remaining elements of an array as a new array.

```javascript
const numbers = [1, 2, 3, 4, 5]
const [n1, n2, ...others] = numbers
console.log(n1, n2, others)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ga0-2.gitbook.io/seifxr10anz-content/week-13/day-3-destructuring-spread-rest-and-ajax/spread-and-rest.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
