1

string in JavaScript are immutable, but when we use 'let' with strings , it becomes changeable, which tends to mean that they are mutable, how can we justify that strings are immutable yet it is mutable when use with let?

1
  • You're changing the variable, not the string. Commented Jul 14, 2023 at 16:50

1 Answer 1

5

Let me help you with that using an example.

Consider we declare a string using let

let someString = 'This is a String'

now you can Re-assign that variable someString to a new value which means let variables can be reassigned.

someString = 'Some new Value'

But Strings are immutable in the sense that you can't mutate the content of the String.

someString[1] = 'A' //This is Invalid since Strings are Immuatable

Happy Coding :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.