FAQ: Arrays - Arrays and Functions

#1

This community-built FAQ covers the “Arrays and Functions” 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 Arrays and Functions

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!

split this topic #2

2 posts were split to a new topic: Where did arr and newArr come from and what do they do or mean?

split this topic #4

2 posts were split to a new topic: Why can .pop() method change the structure of array that was defined as const variable?

#5

Hello everyone. I’m at Arrays and Function, exercise 10. I just want to confirm that what is happening in this exercise is that we are changing the value of the global scope by changing the string ‘mutated’ to ‘MUTATED’ and then .pop it off. Considering where the braces {} are I just want to confirm.

1 Like
#6

Hey everyone, i don’t understand why it is used the ‘=>’ operator before the body of the funcion (to be an operator shouldnt ‘=’ be after the ‘>’ signal?).

const removeElement = newArr => {
newArr.pop() _______________ ^
}

#7

That is arrow function syntax.

const foo = param => {
    // function body
}

Much the same as,

const foo = function (param) {

}

with a few exceptions.

1 Like
#8

We are changing the elements within the array in global scope from within the function’s local/block scope. Even though the scopes are different, the function can do this because of pass-by-reference = if an array is passed into a function using an argument/parameter, any changes to the array’s elements within the function will also be made to the array outside the function. I also rationalise this by remembering that a child’s scope has access to its parent’s and ancestors’ scopes (but not vice versa) i.e. the scope hierarchy.

The first change the function makes is to reassign the value of the fourth element (index position 3) to 'MUTATED'.
The second change is to remove the last element using .pop()pop it off as you say - love it!! :joy::rofl: … which happens to be our friend 'MUTATED' (our old friend 'mutated') again. :wink:

#9

This is because there are not two arrays, but one. The one seen inside the function is the global one referenced in the argument.

1 Like