Question about Understanding String Immutability in JS

Question about Understanding String Immutability in JS
0.0 0


Im wonder if in this challenge, its trying to say that using myStr [0] = " j " for var myStr = “Bob”; would not work but instead we have to use myStr alone without [0]? Does [0] mean targeting first letter of the word Bob but i.e. he tried altering Bob with J so it would give Job instead? Why wouldnt using myStr [0] = “Hello World” work instead? Which means we alter first letter of the word by not only using the new letter of H but instead use whole name of Hello World? Can someone explain this to me a bit more clear?


Think of the string as a set of boxes where each one holds one plushie character. They’d burst if you tried shoving more than one character in the same box. That’s what the bracket syntax implies.

Without the brackets, you are basically saying, here, let myStr actually be the name of this other set of boxes and whatever is in them.


That’s the idea. Some languages allow this because, historically, strings were just arrays of characters. Nowadays, strings are generally treated as an immutable data structure, but you still do this with some languages today. Java still allows changing a string by index if you use a character array rather than a string class. You can still read characters in strings using an index:

var str = "Hello";
str[1]; // "e"

If this were to work, you’d be setting the first element of the myStr array to be an entire string. For instance, if you have an array of single character strings:

var charArray = ['a','b','c','d'];

You could change any one of them by accessing its index

charArray[2] = 'e';
console.log(charArray); // ['a','b','e','d']

If you tried what you’re suggesting, you’d be changing more than just one character:

charArray[0] = "Hello world";
console.log(charArray); // ['Hello world', 'b','e','d']


Yes, it’s easy to think of strings as arrays since you can access them similarly, and indeed in some languages (like C) they are arrays. But in JavaScript, you can read a string like an array but you cannot change it like an array, where you can.

In JavaScript, all primitive data types are immutable - they cannot be changed. I know that sounds weird, but for example when you do

a = 3
a = 4

You aren’t actually changing the value inside a, you’re giving it a completely new value. Similarly, when you want to change a string, you can’t change the value inside it, but have to create a new string and assign it.

myStr = "Hello"
myStr = "Howdy"

If you need to change just one cell, you’ve got to build a new one.

var myStr = 'hovel'

var myStr = myStr.substring(0, 2) + 't' + myStr.substring(3)

// hotel

Yes, it’s a bit awkward, but you get used to it. If you have a lot of insertions to do, you could always build a function to do it.


What does Array mean? Is it a line of text?


Ordered collection of values. In JavaScript syntax is like [1,2,3,4] or ['h', 'e', 'l', 'l', 'o'].


Whoops! Sorry, I didn’t take into account where you were in the course.

Essentially, this stuff will all make sense in no time. Just keep going through the map.