Help! TypeError: Cannot assign to read only property of '0' in string

Help! TypeError: Cannot assign to read only property of '0' in string
0

#1

Hey folks! Can someone please explain to me why I can’t seem to use bracket notation to alter the first character of each item in an array? Everything works fine if if I try:

var a[i] = a[i].toUpperCase():
or
var a[i] = a[i][0].toUpperCase();

but as soon as I try to modify var a[i][x], it yells at me, saying it cannot read only property of ‘x’. Please help me understand why!

(Please note: I’m not asking for the answer to this challenge, if you recognize it. I’m simply wondering why I cannot use bracket notation in this way)


#2

I don’t think a variable name can contain brackets. What you are doing here is very bizarre. I have never seen a variable name with brackets - that’s just not how JavaScript works. Bracket notation can be used on the right side of the assignment operator (=) but not on the left side of it. You should review the basics of variable names.


#3

Because a[i][0] is a specific character of a string and strings are immutable. When you do a[i]= something, it works because you are replacing the entire string and not trying to mutate a part of the string (which is what a[i][0] is attempting to do).


#4

Hint: try replacing the entire string (a[i]) with its first character in upper case concatenated together with a slice of the rest of it in lower case.


#5

Thank you so much for this answer! I was bashing my head into the keyboard over this.