Skip to main content

This is a common confusion with immutability. There's a difference between an immutable value and an immutable reference.

For example, say you have x = 10. You can't do something like 10 = 20 to change the value of x. The 10 is immutable. You can do x = 20, unless x is declared to be immutable. But that doesn't change the value of 10, just the value of x.

On the other hand, you can have const x = SomeObject(), where you can change stuff inside SomeObject, but you can't reassign x to point to a different object.

In most languages, literals likeValues of value types (like numbers and strings in most languages) are immutable, but variables aren't unless declared to be such, even variables pointing to immutable values.

This is a common confusion with immutability. There's a difference between an immutable value and an immutable reference.

For example, say you have x = 10. You can't do something like 10 = 20 to change the value of x. The 10 is immutable. You can do x = 20, unless x is declared to be immutable. But that doesn't change the value of 10, just the value of x.

On the other hand, you can have const x = SomeObject(), where you can change stuff inside SomeObject, but you can't reassign x to point to a different object.

In most languages, literals like numbers and strings are immutable, but variables aren't unless declared to be such, even variables pointing to immutable values.

This is a common confusion with immutability. There's a difference between an immutable value and an immutable reference.

For example, say you have x = 10. You can't do something like 10 = 20 to change the value of x. The 10 is immutable. You can do x = 20, unless x is declared to be immutable. But that doesn't change the value of 10, just the value of x.

On the other hand, you can have const x = SomeObject(), where you can change stuff inside SomeObject, but you can't reassign x to point to a different object.

Values of value types (like numbers and strings in most languages) are immutable, but variables aren't unless declared to be such, even variables pointing to immutable values.

I think "literal" was the word you were looking for for things like 10 or "Hello"
Source Link
Bart van Ingen Schenau
  • 76.7k
  • 20
  • 125
  • 189

This is a common confusion with immutability. There's a difference between an immutable value and an immutable reference.

For example, say you have x = 10. You can't do something like 10 = 20 to change the value of x. The 10 is immutable. You can do x = 20, unless x is declared to be immutable. But that doesn't change the value of 10, just the value of x.

On the other hand, you can have const x = SomeObject(), where you can change stuff inside SomeObject, but you can't reassign x to point to a different object.

In most languages, primitivesliterals like numbers and strings are immutable, but variables aren't unless declared to be such, even variables pointing to immutable values.

This is a common confusion with immutability. There's a difference between an immutable value and an immutable reference.

For example, say you have x = 10. You can't do something like 10 = 20 to change the value of x. The 10 is immutable. You can do x = 20, unless x is declared to be immutable. But that doesn't change the value of 10, just the value of x.

On the other hand, you can have const x = SomeObject(), where you can change stuff inside SomeObject, but you can't reassign x to point to a different object.

In most languages, primitives like numbers and strings are immutable, but variables aren't unless declared to be such, even variables pointing to immutable values.

This is a common confusion with immutability. There's a difference between an immutable value and an immutable reference.

For example, say you have x = 10. You can't do something like 10 = 20 to change the value of x. The 10 is immutable. You can do x = 20, unless x is declared to be immutable. But that doesn't change the value of 10, just the value of x.

On the other hand, you can have const x = SomeObject(), where you can change stuff inside SomeObject, but you can't reassign x to point to a different object.

In most languages, literals like numbers and strings are immutable, but variables aren't unless declared to be such, even variables pointing to immutable values.

Source Link
Karl Bielefeldt
  • 148.4k
  • 38
  • 284
  • 483

This is a common confusion with immutability. There's a difference between an immutable value and an immutable reference.

For example, say you have x = 10. You can't do something like 10 = 20 to change the value of x. The 10 is immutable. You can do x = 20, unless x is declared to be immutable. But that doesn't change the value of 10, just the value of x.

On the other hand, you can have const x = SomeObject(), where you can change stuff inside SomeObject, but you can't reassign x to point to a different object.

In most languages, primitives like numbers and strings are immutable, but variables aren't unless declared to be such, even variables pointing to immutable values.