FAQ: Arrays - Accessing Elements

This community-built FAQ covers the “Accessing Elements” exercise from the lesson “Arrays”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Accessing Elements

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I am not sure whether I have caught another bug in the exercise or whether there is a problem in my code: The second task says:

Now, console.log() the third element in the famousSayings array using bracket notation to access the element.

Do not save the element to a new variable before you log it.

I translated this into the following line of code:

console.log(famousSayings [2]);

The actual program behaves as I would expect it to, printing the third and last saying to the screen, but I don’t get a pass.

Can somebody explain the bug in my brain, please? :slight_smile:

1 Like

Why is the output “W” in the example below?

const hello = ‘Hello World’;
console.log(hello[6]); //output: ‘W’

if index starts with 0, shouldn’t W be index 5?

1 Like

space between character ‘o’ and ‘W’ is also counted.

3 Likes

I am sure you have come right with this by now.

Your issue is that you have a space between famousSayings and [2], it should be like so:

famousSayings[2]

How would I have called the 3rd character instead of the 3rd item in the list?

const famousSayings = ['Fortune favors the brave.', 'A joke is a very serious thing.', 'Where there is love there is life.'];
console.log(famousSayings[2]);
3 Likes

You can use:

console.log(famousSayings[0][2]);

The first bracket [0] looks at the first index in the array, and the second one goes through the characters.

9 Likes

Why does .indexOf return -1 when called with an index not in the array?

how do you ask for multiple indexes within an array? I tried console.log(famousSayings[0, 1, 2]); but it doesn’t work.

Blockquote

If you want to access multiple consecutive items you can use slice or splice. I assume this course will go over both of these. These both return arrays.

Here is a blog explaining slice and splice. JavaScript Array.splice() vs Array.slice() | TO THE NEW Blog

If you want to log individual items from an array you could use this:

console.log(famousSayings[0], famousSayings[1], famousSayings[2] );

Another good resource to learn more about arrays is Array - JavaScript | MDN

2 Likes

It’s odd that the ’ isn’t counted. You would think anything after the = would be counted as some sort of index. My first thought was that it was, then I was going to ask why it wasn’t [7] but this clears that up.

I was wondering the same thing, since the example would lead one to believe that famousSayings[2] should print “r”.

cloudace’s method is the most direct way to do it. Another way I discovered is to print from listItem:

const listItem = famousSayings[0];
console.log(listItem[2]); // prints 'r'

An array of strings is essentially a two-dimension array, hence the second index (subscript) to access the nested iterable (the string).

Is it exercise 3? Can you show us the code you passed with?

nvm it was part of the code. But thank you for your assistance :grin:

1 Like

That shouldn’t be a problem. Most developers prefer leaving no space between the array and the square brackets. However, there won’t be an error if you do

This does appear to be a bug. Since arrays in JS are zero-indexed, printing the first element would be done with ‘0’ in the brackets; the second element using [1]; and the third element using [2].

For some reason, the exercise requires you to input [3], which results in an undefined message but marks the exercise correct.

it is not a bug, but the intention of the author to demonstrate what happens when we attempt to access an element that does not exist. The index is said to be out of range.


hello ,
could someone help me please ? i am reading about arrays and thought something which i am not very clear . It is about Method of .push and .pop. As you can see in the example, when we want to add new element ‘pear’ , it calls variable directly cart.push(‘pear’); whereas for .pop, they call the new variable name ‘const poppedIngredient = Ingredients.pop(); ’ . My question is ’ do we need to call new variable name ‘poppedingredient’ ? Can we not simply say ingredients.pop(); ? Vice versa, in .push method, can we call new variable name like ‘pushedCart’=cart.push(pear);’ ?

thanks

In that instance, pushedCart will be the new length of the array.

In that instance poppedIngredient will be the value taken off the array.

arr = ['one', 'two', 'three']
newArrLength = arr.push('four')
console.log(newArrLength)
poppedValue = arr.pop()
console.log(poppedValue)

Output

4
four