DEV Community

Discussion on: #100DaysOfVanillaJS: All About Strings, String Methods, & Template Literals

Collapse
 
surfwoodroad profile image
Michael Roberts

Thanks for this series!
In mentioning the immutability of strings, it's probably also worth mentioning for the newbies that the alternative is you just reassign the value of the variable in small tasks where you won't need the original value again (and if you need to keep the original value, you assign the new value to a new variable):
let faveQ = "The Marathon Continues";
faveQ
"The Marathon Continues"

faveQ = faveQ.slice(4);
"Marathon Continues"

faveQ
"Marathon Continues"

So, in my case above, it kinda looks like you could be mutating the string (and simply editing the text), but you're really reassigning a new string to the variable.