2018-09-30 23:01:58 +01:00
---
id: 56bbb991ad1ed5201cd392ca
title: Access Array Data with Indexes
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cBZQbTz'
2019-07-31 11:32:23 -07:00
forumTopicId: 16158
2021-01-13 03:31:00 +01:00
dashedName: access-array-data-with-indexes
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2019-10-27 15:45:37 -01:00
We can access the data inside arrays using < dfn > indexes< / dfn > .
2020-11-27 19:02:05 +01: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` .
< br >
**Example**
2019-05-17 06:20:30 -07:00
```js
2021-10-26 01:55:58 +09:00
const array = [50, 60, 70];
2022-03-30 11:45:20 -07:00
console.log(array[0]);
2021-10-26 01:55:58 +09:00
const data = array[1];
2019-05-17 06:20:30 -07:00
```
2022-03-30 11:45:20 -07:00
The `console.log(array[0])` prints `50` , and `data` has the value `60` .
2021-03-02 16:12:12 -08:00
2020-11-27 19:02:05 +01:00
# --instructions--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
Create a variable called `myData` and set it to equal the first value of `myArray` using bracket notation.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
The variable `myData` should equal the first value of `myArray` .
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(
(function () {
if (
typeof myArray !== 'undefined' & &
typeof myData !== 'undefined' & &
myArray[0] === myData
) {
return true;
} else {
return false;
}
})()
);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
The data in variable `myArray` should be accessed using bracket notation.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(
(function () {
if (code.match(/\s*=\s*myArray\[0\]/g)) {
return true;
} else {
return false;
}
})()
);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --after-user-code--
2018-09-30 23:01:58 +01:00
```js
2018-10-20 21:02:47 +03:00
if(typeof myArray !== "undefined" & & typeof myData !== "undefined"){(function(y,z){return 'myArray = ' + JSON.stringify(y) + ', myData = ' + JSON.stringify(z);})(myArray, myData);}
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
2021-10-26 01:55:58 +09:00
const myArray = [50, 60, 70];
2018-09-30 23:01:58 +01:00
2021-04-08 11:36:05 -05:00
2020-11-27 19:02:05 +01:00
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2021-10-26 01:55:58 +09:00
const myArray = [50, 60, 70];
const myData = myArray[0];
2018-09-30 23:01:58 +01:00
```