1

I am bit confused, Wikipedia (I know, I know) defines immutability as

immutable object is an object whose state cannot be modified after it is created.

while the JS implementation example states

In JavaScript, some built-in types (numbers, strings) are immutable

I understand now that in JS both Numbers and Strings are Objects but why would they be considered immutable when I can clearly change the value of any Number or String variable?

9
  • 9
    You "change" by creating a new string or number, not by modifying the original Commented Apr 21, 2015 at 14:14
  • 3
    You are talking about assigning different values to variables. Commented Apr 21, 2015 at 14:14
  • var x = 42 assigns an immutable number/value 42 to your variable. if you do x++; then the 42 is destroyed and replaced with a new immutable number: 43. Commented Apr 21, 2015 at 14:15
  • You can't change the value of e.g. 8 or 23 or "puppies!". Commented Apr 21, 2015 at 14:16
  • 2
    The string/number is immutable. The variable you're storing it in is not. Commented Apr 21, 2015 at 14:17

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.