# Arrays

An array is a data structure that contains any number of items (or `elements`). In JavaScript, array elements can be of any data type.

Create an empty array:

```javascript
const arr = []
```

Elements in the array should be separated by commas.

```javascript
const satchel = [
  'chair',
  'table',
  'candle',
  'map',
  'magnifying glass',
  'rupees',
  'Quick Eze',
  'boomerang'
]

const squares = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
```

#### Accessing Elements

Each element in the array has a numbered `index`. The first element has an index of `0`.

Access elements by putting the `index number` in square brackets.

```javascript
const satchel = [
  'chair',
  'table',
  'candle',
  'map',
  'magnifying glass',
  'rupees',
  'Quick Eze',
  'boomerang'
]

console.log(satchel[0]) // => 'chair'
console.log(satchel[1]) // => 'table'
```

#### Getting the Number of Elements in an Array

We can use the `.length` property to find out the number of elements in an array

```javascript
const satchel = [
  'chair',
  'table',
  'candle',
  'map',
  'magnifying glass',
  'rupees',
  'Quick Eze',
  'boomerang'
]

console.log(satchel.length) // => 8
```

#### Changing Elements

* To change an element in an array, first access the element, and then assign a new value:

```javascript
const satchel = [
  'chair',
  'table',
  'candle',
  'map',
  'magnifying glass',
  'rupees',
  'Quick Eze',
  'boomerang'
]

satchel[2] = 'glowing orb'

console.log(satchel)
```

**NOTE**: `const` only prevents reassignment, the value can still be mutated.

### Iterate over an array

Let's use a `for..of` loop to iterate over the array and print each element out.

```javascript
for (let item of satchel) {
  console.log(`A nimble rogue is trying to steal my ${item}!!!1`)
}
```


---

# Agent Instructions: 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-2/day-1-creating-elements-and-arrays/arrays.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.
