www.webdeveloper.com
Results 1 to 4 of 4

Thread: Primitive values immutable or not?

  1. #1
    Join Date
    Jun 2009
    Posts
    28

    Question Primitive values immutable or not?

    Hello

    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:
    Code:
    "use strict";
    
    var myvar = "hello, world!";
    myvar = "changed!";
    
    function myfunc(){
    	console.log("Before");
    	//alert(myvar);
    	document.write(myvar);
    	console.log("After");
    }
    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
    Location
    Behind you...
    Posts
    868
    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
    Location
    Sweden
    Posts
    227
    To demonstrate further:

    Code:
    // 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
    New to web development or in need of a good reference? Check out the Mozilla Developer Network or W3Schools.

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    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 02:01 AM.

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



Recent Articles