7
SD010
8y

So a 'const' value can definitely change :|

JavaScript ES6 :|

Comments
  • 0
    Sorry one more JS rant, can't help it

    (earlier saw a rant about 'Javascript infested water') :/
  • 6
    that is perfectly correct since "const" only prevents for REASSIGN the variable. The foo var in your screenshot is an array that has been ADDED a key-value pair, but the array hasn't been reassigned :)
  • 0
    @javierojeda

    You means to say object
    Yes you are right its not reassigned but i just mentioned the words of that tweet
  • 2
    oh yeah, it's an object sorry :p but I've seen a very large number of people saying that "const" is a stupid thing, however it is just a misconception of what the variable type actually does... some developers need to read more u.u
  • 0
    @javierojeda

    Ohh yes you are right, I need to read more ;)
  • 0
    Const does not mean the contents of an object can't change, it just means you can't change the pointer to point to another object.

    Haven't done C++ in awhile but I think a similar concept would apply if you exposed a const property that is an object, you can't reassign to another object but you can call calls on that object that change its state.
  • 1
    @kheftel

    Yes

    But to make it immutable we can use freeze method, it will not even allow to change the value of an Object during run time

    Const is officially just introduced in ES6 so many people are not aware about it but freeze is an ES5 - ES5.1 method
  • 1
    It just prevent you to change the reference of that variable. Most people think that const is "immutable", where it clearly isn't
Add Comment