-1

can someone help me understand this piece of code?

let word = 'Nose';
word[0] = 'P';

console.log(word); // logs as "Nose"
console.log(word[0]); // logs as "N"

Shouldn't word[0] be 'P' and word should be 'Pose' right?

Prem
  • 51
  • 1
  • 7

1 Answers1

0

Strings are immutable in javascript. They cannot be mutated but the variable can be reassigned a new value.

let str = "Hello";
str[0] = "M";
console.log(str);
str = "Mello";
console.log(str);
Tushar Shahi
  • 10,769
  • 1
  • 9
  • 29