FAQ: Arrays - Update Elements

This community-built FAQ covers the "Update 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 _Update 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!

Why can’t I just delete fall in the var at the beginning and write Autumn instead of writing a new command to replace it? Isn’t that just creating redundant code?

3 Likes

Dont quote me but im guessing its for if you want the code to be fall when its first read but then change to autumn later, im guessing theres a use for it somewhere i havent googled.

I changed the type of the groceryList to const, and still can change the elements of the array without errors, despite it is constant. as below, so how come?

const groceryList = [‘bread’, ‘tomatoes’, ‘milk’];

groceryList [1] = ‘avocados’;

console.log(groceryList);

// output: [ ‘bread’, ‘avocados’, ‘milk’ ]

2 Likes

had the same question… Please reply ASAP

You can change the content of an array assigned to a const variable, and the type of variable would still be an array and the array, the list would be the same, just different content.

You can think of it as a list on a piece of paper you named List1, but you erased an item and wrote something else in place.

What you would’t be allowed to do, is to turn this variable into another array or another type of variable, say a boolean or a string.

Like you can’t take another piece of paper or a rock and call it List1.

Don’t trust me blindly, I’m also learning.

Nice question, When you use const for a collection variable(group of elements under one variable (like array or objects) ), You can still edit the values of the collection… But when you use const for single variable , you cannot update its values…

In documentation says this in crisp way i.e const cannot be re-assigned or re-declared …


//+ Possible updation in collection

const x = {};

x.foo = 'bar';
console.log(x); // {foo : 'bar'}

x.foo = 'bar2';
console.log(x); // {foo : 'bar2'}  

const y = [];
y.push('foo');
console.log(y); // ['foo']

y.unshift("foo2");
console.log(y); // ['foo2', 'foo']

y.pop();
console.log(y); // ['foo2']

Updation not possible for non-collections.


const x = {};
x = {foo: 'bar'}; // error - re-assigning

const y = ['foo'];
const y = ['bar']; // error - re-declaring

const foo = 'bar'; 
foo = 'bar2';       // error - can not re-assign

var foo = 'bar3';   // error - cannot re-declare
function foo() {};  // error - cannot re-declare

I hope you are understood now !

1 Like

yeah you can do that… But the purpose of this tutorial to learn updation of element in array … changing values straight in the input array is not called updation.

I love your explanation! But then which should we rather use for defining array variables? ‘let’ or ‘const’?

Its based on the usage…If you want to re-declare the variable content , use let otherwise use const…

why does the below code log “bread, avocados and milk” to the console?

let groceryList = [‘bread’, ‘tomatoes’, ‘milk’];
groceryList[1] = “avocados”;

BUT the code below logs only “avocados” to the console

let groceryList = [‘bread’, ‘tomatoes’, ‘milk’];

let modifiedGroceryList = groceryList[1] = “avocados”

Thanks you

if you use modifiedGroceryList variable in console then it will log only avocados bcuz you update an element in array and store it in modifiedGroceryList variable it only show you updated elements and on the other hand if you use groceryList variable in console it will log whole array with avocados bcuz you update the element in groceryList variable and then console it so it show you array with updated element i hope that’s help you