DEV Community

Discussion on: Var, let and const- what's the difference?

Collapse
 
olawanle_joel profile image
Joel Olawanle • Edited

const alone does not guarantee protection for your data or let me say it does not protect your data from mutation,

Example:

const GREETING = {
    name : "Joel",
    info : "Goodday!"
 }
Enter fullscreen mode Exit fullscreen mode

Though the above code makes use of const i can still update the values via the code below

GREETING.name = "Elijah";
Enter fullscreen mode Exit fullscreen mode

But this could easily be avoided by making use of Object.freeze() to freeze our const variables.
Just add this line of code and you will discover that the values cannot be updated.

Object.freeze(GREETING); 

GREETING.name = "Elijah"; // This will now be ignored due to mutation
Enter fullscreen mode Exit fullscreen mode

For more clarification check: freecodecamp.org/learn/javascript-...