1

I understand the strings being immutable, but Im not sure when that is. For example, if Im writing a code on notepad, obviously I can go back and change the string. At what point do you stop being able to change it?

2
  • 4
    A string is always immutable in JavaScript. You can't modify it. You can't go to a specific position and change the value. You can only create a new string and assign it to the old variable. Commented Jul 17, 2021 at 1:05
  • Absolute immutability can be obtained once the devices hosting the code are destroyed, you can't escape string mutability in other cases. The solution is a hammer. Commented Jul 17, 2021 at 1:11

1 Answer 1

3

Something being immutable does not affect how you alter it in your editor-- it is immutable meaning that it cannot be changed in the execution of the code. And by this, we mean only that by changing its value, we are effectively creating a new thing. This differs from objects and arrays, which can have their value changed while remaining the same object. For example:

const object1 = { hello: 'world' };
const object2 = object1;

console.log('object1->', object1);
console.log('object2 ->', object2);

console.log('object1 === object2 ->', object1 === object2); // will be true
console.log('mutating object1...');
object1.hello = 'Shachar';

console.log('object1 has changed ->', object1);
console.log('object2 has changed->', object2);

console.log('object1 === object2 ->', object1 === object2); // they both still reference the same object

Both object1 and object2 are pointing to a reference of the same object. This object can be mutated by changing the property of object1's reference and object2 will reflect this mutation; because, again, they are both referencing the same object.

The same is not true of strings. For this reason a string cannot be changed in place with any methods. You can append a value with operations or string methods, but in doing so you are creating new objects, so you will need to reassign it to the variable, losing the reference to the original string:

let string1 = 'hello';
let string2 = string1;

console.log('string1 ->', string1);
console.log('string2 ->', string2);

console.log('string1 === string2 ->', string1 === string2); // true

string1.concat(' world!');
console.log('string1 ->', string1); // still just 'hello', because `concat` doesn't (and cannot) mutate the original string

string1 = string1.concat(' world!'); // change by reassignment
console.log('string1 ->', string1); // *now* it reflects the change

// but now string 1 and string 2 are different
console.log('string1 ->', string1);
console.log('string2 ->', string2);

// and not equal
console.log('string1 === string2 ->', string1 === string2);

// also, since they are immutable, strings are just compared by *value*, so we can do this

console.log('"hello" === string2 ->', "hello" === string2); //true

It can be a bit tricky at first to understand how all of this works. A few key points to remember:

  1. Immutability doesn't refer to how they are handled in the editor; it refers to how they are handled in the JS execution environments
  2. Objects and Arrays are passed by reference and are compared by reference, and can be mutated while keeping the original reference
  3. Other types are scalar and immutable. They are compared by value, not reference. They cannot have their value changed-- one can only create new instances (although a variable can be made to point to any number of new instances if it is a let or var).

I hope this provides some clarity.

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.