I have following simple javascript code:
s="hello"
s[0]="X"
console.log(s[0])
The output is h
, but I have updated it as X
, I would ask why I got such result, thanks.
I have following simple javascript code:
s="hello"
s[0]="X"
console.log(s[0])
The output is h
, but I have updated it as X
, I would ask why I got such result, thanks.
Strings in JavaScript are primitives, which means they are immutable, ie. you are not able to change them. If you were running your code in strict mode then this would throw an error.
If you wanted to modify this string you would need to effectively create a copy of it, and then assign that to the s
variable eg.
s = "X" + s.substring(1)