FAQ: Arrays - Arrays with let and const

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

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!

Hi,
I just completed the " Arrays with let and const" section and I must say I don’t get the explanation that says : " However, if a const variable contains an array, you can mutate elements in the array but not re-assign an new value!"
–> What we were asked to do in the exercice consisted (to me) to reassign a value to an element stored in a const variable… Which is way I don’t get the subtelty between “mutating” an element and “re-assigning it a new value”. Can someone help me on this ? :slight_smile:

5 Likes

lets do analogy, lets say you build a house (which represents a an array declared with constant):

const myHouse = ['living_room', 'bath_room', 'kitchen'];

you can perfectly change things in the house, add a dishwasher for example:

const myHouse =  ['living_room', 'bath_room', 'kitchen'];
myHouse.push('dishwasher');

however, you can’t just replace the house:

const myHouse = ['living_room', 'bath_room', 'kitchen'];
myHouse = ['new_house']; // error

so that is the difference between mutation (manipulating the array itself), and re-assigning the variable.

28 Likes

Hello, i was working with let and const but i deleted the elements in the condiments array. I restored my worksheet but it didn’t restore to the original array but the one i deleted. Kindly help with the elements in the condiment array!!!

Here is what comes up in the lesson, for me. Is this what you are looking for?

let condiments = ['Ketchup', 'Mustard', 'Soy Sauce', 'Sriracha'];

17 posts were split to a new topic: Difference between const and let

My only question from this lesson is about logging in changes. When I change my element in my array the exercise instructed me to log in the change. When we change in a array are we suppose to console.log() the change always?

That’s not a requirement, only suggested in the exercise so you can see the change. When we make a change to any element in an array, the change takes place. Once we are certain that our syntax is correct and we’re using a valid approach, we can expect the change.

There are several ways to manipulate an array. Study the Array methods listed on MDN to get an idea of what tools are at our disposal.

1 Like

Hi there,

I was wondering why when we reassign the variable ‘condiments’ to a new array ([‘Mayo’]) this does not change the output from the first console.log command. Pardon my error in coding parlance, but why when we change the variable ‘condiments’ to a new array does this not have global scope?

let condiments = ['Ketchup', 'Mustard', 'Soy Sauce', 'Sriracha'];

condiments[0]='Mayo'

console.log(condiments)

condiments=['Mayo']

console.log(condiments)

Thanks very much!

1 Like

The code still runs from top to bottom, when the first console.log is reached, you haven’t yet re-assigned the array

1 Like

Thanks for responding!

I wonder if you could expand on this topic a bit for me. In the previous module we wrote some functions that called on helper functions outwith the function code block that were below that function code block.
If the variable doesn’t exist until it has been “read” (parsed?) top to bottom (or doesnt change unless the change is above the console.log, as in my example above), how can a function use a helper function that is below it in the code?

(P.S the hint number One in Arrays 5/12 says ‘Hans Solo’… unforgivable)

What does that have to do with question? There are no functions within your code.

True, functions would change the order of your code, given function can be executed later or not at all (if you never call the function)

so this code just gets executed from top to bottom, there is no “jumps” anywhere in the code.

I am just getting my head around why variable require to be assigned above where they can be used (as in my example) but functions (in other situations, not this one) can be called before they are declared.

for example:
the following works

functionName()

function functionName (){
console.log(‘hello’)
}

but this doesnt

console.log(variableName)

let variableName=‘hello’

Im not sure I understand why this is

variable and function hoisting, not my favorite part of the JS language, so i will let someone else do the talking:

http://adripofjavascript.com/blog/drips/variable-and-function-hoisting.html

1 Like

Thank you very much, very helpful :+1:

on the first task, the answer is
condiments[0]= ‘Mayo’

on the first try I wrote
let condiments[0]= ‘Mayo’ and it was wrong

why was adding let wrong?

1 Like

let is used when declaring a variable, given the variable its scope (among other things)

when updating variable or elements in array, don’t use let (or var and const for that matter)

1 Like

Thank you! I’ll remeber that

Step 2 in this exercise is to reassign condiments to be a new array with a single string [‘Mayo’]. I used the let keyword in front of condiments with ‘Mayo’ in the square brackets but got an error saying that condiments was already declared. When i removed the keyword let, the code returned the anticipated answer and I could move onto Step 3. Why isn’t let used to reassign condiments to a new array when it is used initially to create the array?

Because the variable has already been declared. All we need do after that is change the assignment. We do not re-declare variables.


Consider the for loop…

for (let i = 0; i < 10; i++) {
    console.log(i + 1)
}

i is not continuously declared in this loop. That directive in the for argument is never re-visited. After that it only gets new values assigned to it until such time as it fails the loop condition.

i++

is equatable to,

i = i + 1

What’s important to see here is the expression on the right. It is a new value. When we assign that, the old value disappears.

1 Like