Strings and const variables

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

I have a question about const variables and strings. Since strings are immutable meaning they can’t be changed like this:

let myString = 'foo';
myString[0] = 'b';

and variables declared with const can’t be changed like this:

const myArray= [1,2,4];
myArray = [1,2,5]; 

Can I then do this:

const myString = 'foo';
myString[0] = 'b' 



  **Your browser information:**

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

Challenge: Mutate an Array Declared with const

Link to the challenge:

No you cannot. It doesn’t matter how a string variable is declared, it is immutable. For example, the following strings cannot be mutated.

const firstName = 'John';
let surName = 'Doe';
var otherName = 'Jane';

Any variable declared with let or var key word can be reassigned. However variables declared with const keyword cannot be reassigned. This is not limited to string variables. Don’t confuse mutation with reassignment.


No you can’t because that had still defeat the immutability rule to my best understanding.
String is practically immutable.
you can however change arrays like this:
const myString = [[“foo”], [“simic21”]]
and you would have [[“veronica”], [“simic21”]]

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