Files
.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
access-an-arrays-contents-using-bracket-notation.english.md
access-property-names-with-bracket-notation.english.md
add-items-to-an-array-with-push-and-unshift.english.md
add-items-using-splice.english.md
add-key-value-pairs-to-javascript-objects.english.md
check-for-the-presence-of-an-element-with-indexof.english.md
check-if-an-object-has-a-property.english.md
combine-arrays-with-the-spread-operator.english.md
copy-an-array-with-the-spread-operator.english.md
copy-array-items-using-slice.english.md
create-complex-multi-dimensional-arrays.english.md
generate-an-array-of-all-object-keys-with-object.keys.english.md
iterate-through-all-an-arrays-items-using-for-loops.english.md
iterate-through-the-keys-of-an-object-with-a-for...in-statement.english.md
modify-an-array-stored-in-an-object.english.md
modify-an-object-nested-within-an-object.english.md
remove-items-from-an-array-with-pop-and-shift.english.md
remove-items-using-splice.english.md
use-an-array-to-store-a-collection-of-data.english.md
use-the-delete-keyword-to-remove-object-properties.english.md
basic-javascript
debugging
es6
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-quality-assurance
07-scientific-computing-with-python
08-data-analysis-with-python
09-information-security
10-coding-interview-prep
11-machine-learning-with-python
12-certificates
portuguese
russian
spanish
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
lib.js
md-translation.js
package-entry.js
package-lock.json
package.json
utils.js
cypress
docs
search-indexing
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
cypress-install.js
cypress.json
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
jest.config.js
lerna.json
package-lock.json
package.json
sample.env
freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-data-structures/access-an-arrays-contents-using-bracket-notation.english.md

88 lines
2.8 KiB
Markdown
Raw Normal View History

---
id: 5a661e0f1068aca922b3ef17
title: Access an Array's Contents Using Bracket Notation
challengeType: 1
isHidden: false
forumTopicId: 301149
---
## Description
<section id='description'>
The fundamental feature of any data structure is, of course, the ability to not only store data, but to be able to retrieve that data on command. So, now that we've learned how to create an array, let's begin to think about how we can access that array's information.
When we define a simple array as seen below, there are 3 items in it:
2019-04-26 17:21:49 -07:00
2019-04-26 17:21:49 -07:00
```js
2019-04-26 17:21:49 -07:00
let ourArray = ["a", "b", "c"];
```
In an array, each array item has an <dfn>index</dfn>. This index doubles as the position of that item in the array, and how you reference it. However, it is important to note, that JavaScript arrays are <dfn>zero-indexed</dfn>, meaning that the first element of an array is actually at the <em><strong>zeroth</strong></em> position, not the first.
In order to retrieve an element from an array we can enclose an index in brackets and append it to the end of an array, or more commonly, to a variable which references an array object. This is known as <dfn>bracket notation</dfn>.
For example, if we want to retrieve the <code>"a"</code> from <code>ourArray</code> and assign it to a variable, we can do so with the following code:
2019-04-26 17:21:49 -07:00
2019-04-26 17:21:49 -07:00
```js
2019-04-26 17:21:49 -07:00
let ourVariable = ourArray[0];
// ourVariable equals "a"
```
In addition to accessing the value associated with an index, you can also <em>set</em> an index to a value using the same notation:
2019-04-26 17:21:49 -07:00
```js
2019-04-26 17:21:49 -07:00
ourArray[1] = "not b anymore";
// ourArray now equals ["a", "not b anymore", "c"];
```
Using bracket notation, we have now reset the item at index 1 from <code>"b"</code>, to <code>"not b anymore"</code>.
</section>
## Instructions
<section id='instructions'>
In order to complete this challenge, set the 2nd position (index <code>1</code>) of <code>myArray</code> to anything you want, besides <code>"b"</code>.
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: <code>myArray[0]</code> should be equal to <code>"a"</code>
testString: assert.strictEqual(myArray[0], "a");
- text: <code>myArray[1]</code> should not be equal to <code>"b"</code>
testString: assert.notStrictEqual(myArray[1], "b");
- text: <code>myArray[2]</code> should be equal to <code>"c"</code>
testString: assert.strictEqual(myArray[2], "c");
- text: <code>myArray[3]</code> should be equal to <code>"d"</code>
testString: assert.strictEqual(myArray[3], "d");
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
let myArray = ["a", "b", "c", "d"];
// Only change code below this line
// Only change code above this line
console.log(myArray);
```
</div>
</section>
## Solution
<section id='solution'>
```js
let myArray = ["a", "b", "c", "d"];
myArray[1] = "e";
```
</section>