Basic JavaScript - Modify Array Data With Indexes

Tell us what’s happening:
Describe your issue in detail here.

Your code so far

// Setup
const myArray = [18, 64, 99];

// Only change code below this line

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Modify Array Data With Indexes

Link to the challenge:

I don’t really have a problem, I’m just trying to understand the logic behind Javascript better. In this lesson, it says that the array can be changed even though it was declared as a const. So what I’m wondering is, why not declare it as a var then? I mean, what’s the added value of using a const in this scenario then?

Hey, thanks for your reply. I understand that but that wasn’t really what I meant though. I get that a const can’t be reassigned, hence my question. SINCE a const can’t be reassigned, then why use it in this case as we ARE reassigning it? So, why not just make it a ‘var’?

*edit

Sorry I just realized that I didn’t post the lesson, only the part I had to edit. Below is the lesson text:

Unlike strings, the entries of arrays are mutable and can be changed freely, even if the array was declared with const.

Example

const ourArray = [50, 40, 30];
ourArray[0] = 15;

ourArray now has the value [15, 40, 30].

Mutating a data structure and reassigning the variable is not the same thing.

// Mutation
const arr = [1, 2, 3]
arr[3] = 4;

// Reassignment
let arr2 = [1, 2, 3]
arr2 = [1, 2, 3, 4]

There have been a few complaints about how the const keyword might signal the wrong intent. There are people that (almost) never use it because they don’t think it brings many benefits and let is faster to write, so they just use let for everything. E.g. if you look at the react-router source code you can see a lot of lets.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.