Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
The reason to use const instead of let is because you want your code to be immutable. (just a note, const doesn't make object immutable, you have to freeze).
So how do you eat your groceries while being immutable?
Try to view immutable code as code with the added dimension of TIME!
// the pastconst{flour,eggs,turkey}=groceries// the presentconstcookedEggs=cookEggs(eggs)// the futureconstcookTurkey=turkey=>({...turkey,cooked:true})
At any moment in our application we can go back and look at what the state was before.
So imagine at 12:00 you had raw turkey. then at 12:30 you had cooked turkey. This is how immutable code works.
With mutable code, you know you have cooked turkey, but your past is invisible to you. You can never look at a previous state.
const is a good practice, but you even continue using let with immutable code. Sometimes itโs easier and more readable to use let for an immutable object, to which you are going to apply transforms.
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
@joelnet
I still don't need cookTurkey instead I can do turkey.cooked = true and so it doesn't matter whether I use const or let if my code is written keeping immutability in mind.
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
that is kind of why I mentioned '(just a note, const doesn't make object immutable, you have to freeze)' was to distinguish between const and immutable.
If you want a primitive value to be immutable, const is enough. If you want an object to be immutable, you have to freeze it as well.
So if we can assume groceries is immutable (which is should be).
The reason to use
const
instead oflet
is because you want your code to be immutable. (just a note,const
doesn't make object immutable, you have tofreeze
).So how do you eat your groceries while being immutable?
Try to view immutable code as code with the added dimension of TIME!
At any moment in our application we can go back and look at what the state was before.
So imagine at 12:00 you had raw turkey. then at 12:30 you had cooked turkey. This is how immutable code works.
With mutable code, you know you have cooked turkey, but your past is invisible to you. You can never look at a previous state.
Thanks joelnet for following up with technical aspect of the code and the implication of using
const
.const is a good practice, but you even continue using let with immutable code. Sometimes itโs easier and more readable to use let for an immutable object, to which you are going to apply transforms.
sure.
i just try my best to make everything immutable and handle mutable state separately, like in a redux store.
@joelnet I still don't need
cookTurkey
instead I can doturkey.cooked = true
and so it doesn't matter whether I useconst
orlet
if my code is written keeping immutability in mind.that is kind of why I mentioned '(just a note, const doesn't make object immutable, you have to freeze)' was to distinguish between const and immutable.
If you want a primitive value to be immutable,
const
is enough. If you want an object to be immutable, you have to freeze it as well.So if we can assume
groceries
is immutable (which is should be).Then your groceries will truly be immutable
Sorry. Misunderstood as the freeze wasn't mentioned in the previous code. Thanks for clearing it up! :)