2018-10-10 18:03:03 -04:00
---
id: 56bbb991ad1ed5201cd392ca
2021-02-06 04:42:36 +00:00
title: Access Array Data with Indexes
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-04-29 18:29:13 +08:00
videoUrl: 'https://scrimba.com/c/cBZQbTz'
forumTopicId: 16158
2021-01-13 03:31:00 +01:00
dashedName: access-array-data-with-indexes
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
We can access the data inside arrays using < dfn > indexes< / dfn > .
2021-01-13 09:11:33 -08:00
2021-02-06 04:42:36 +00:00
Array indexes are written in the same bracket notation that strings use, except that instead of specifying a character, they are specifying an entry in the array. Like strings, arrays use < dfn > zero-based</ dfn > indexing, so the first element in an array has an index of `0` .
2020-04-29 18:29:13 +08:00
2021-02-06 04:42:36 +00:00
< br >
**Example**
2020-04-29 18:29:13 +08:00
```js
var array = [50,60,70];
2021-02-06 04:42:36 +00:00
array[0]; // equals 50
var data = array[1]; // equals 60
2020-04-29 18:29:13 +08:00
```
2021-02-06 04:42:36 +00:00
**Note**
There shouldn't be any spaces between the array name and the square brackets, like `array [0]` . Although JavaScript is able to process this correctly, this may confuse other programmers reading your code.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Create a variable called `myData` and set it to equal the first value of `myArray` using bracket notation.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The variable `myData` should equal the first value of `myArray` .
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(
(function () {
if (
typeof myArray !== 'undefined' & &
typeof myData !== 'undefined' & &
myArray[0] === myData
) {
return true;
} else {
return false;
}
})()
);
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
The data in variable `myArray` should be accessed using bracket notation.
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(
(function () {
if (code.match(/\s*=\s*myArray\[0\]/g)) {
return true;
} else {
return false;
}
})()
);
2018-10-10 18:03:03 -04:00
```
2021-01-13 03:31:00 +01:00
# --seed--
## --after-user-code--
```js
if(typeof myArray !== "undefined" & & typeof myData !== "undefined"){(function(y,z){return 'myArray = ' + JSON.stringify(y) + ', myData = ' + JSON.stringify(z);})(myArray, myData);}
```
## --seed-contents--
```js
// Setup
var myArray = [50,60,70];
// Only change code below this line
```
2020-12-16 00:37:30 -07:00
# --solutions--
2020-04-29 18:29:13 +08:00
2021-01-13 03:31:00 +01:00
```js
var myArray = [50,60,70];
var myData = myArray[0];
```