Results 1 to 4 of 4

Thread: Primitive values immutable or not?

  1. #1
    Join Date
    Jun 2009

    Question Primitive values immutable or not?


    I'm reading David Flanagan's "JavaScript: The Definitive Guide 6th ed", which says on page 44:
    There is a fundamental difference in JavaScript between primitive values (undefined, null, booleans, numbers, and strings) and objects (including arrays and functions). Primitives are immutable: there is no way to change (or “mutate”) a primitive value.
    And still, the following code works:
    "use strict";
    var myvar = "hello, world!";
    myvar = "changed!";
    function myfunc(){
    I expected to get an error with "myvar="changed!";" but it works. Did I misunderstand what is meant with primitives being immutable?

    Thank you.

  2. #2
    Join Date
    Mar 2005
    Behind you...
    Based on my understanding of this, it isn't so much that you are changing the value of let's say 'myvar', but you are setting it to an entirely new value. The original value is erased entirely and it is replaced with something new. For objects you are actually changing values as the object itself remains intact (otherwise you'd lose all other information within the object).

    Hopefully that helps clear up the difference between primitive values and objects.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  3. #3
    Join Date
    Aug 2008
    To demonstrate further:

    // Using numbers (which are primitive)...
    var myFirstNumber = 4;
    var myOtherNumber = myFirstNumber;
    myOtherNumber = 9;
    alert(myFirstNumber); // alerts 4 - myFirstNumber was not affected
    // Conclusion: myFirstNumber and myOtherNumber are not the same
    // Using objects...
    var myFirstObject = {};
    var myOtherObject = myFirstObject;
    myFirstObject.myAttribute = 4
    myOtherObject.myAttribute = 9;
    alert(myFirstObject.myAttribute); // alerts 9 - myFirstObject was affected!
    // Conclusion: both myFirstObject and myOtherObject represents the same object

  4. #4
    Join Date
    Jul 2008
    urbana, il
    you can reassign a variable at will, that's no biggie.
    what you can't do is modify it without the re-assignment, or simply without using the "=" char...

    with an array, you can say x=[1,2,3]; x.reverse(); alert(x[0]); to see 3.
    you can't do that with a string: all the String.prototoype method like bold() and replace() return new strings: the value to the left of the dot that's to the left of the method call never changes, even if something different comes out of the right-hand side of the method call.

    contrast this with arrays, where a pop() or push() changes the value to the left of the dot that's to the left of the method call.

    one more way of saying it: if something is immutable, you cannot create a prototype method that alters the 'this' value, only one that returns something new.

    this is related to, but not the same as the passing byRef and byVal effects described in post #3.
    Last edited by rnd me; 01-04-2013 at 03:01 AM.
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center