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
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
var array = [50,60,70];
array[0]; // equals 50
var data = array[1]; // equals 60
```
2020-11-27 19:02:05 +01: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-09-30 23:01:58 +01: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
// Setup
var myArray = [50,60,70];
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
// Only change code below this line
```
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
var myArray = [50,60,70];
var myData = myArray[0];
```