Table of contents
No headings in the article.
Will a const declaration protect your data from mutation? Well...not really!
Let's say I have an array with the following elements 7,2,5 and I have a function named "mutate"
const numbers = [7,2,5,]
function mutate (){ numbers.sort() console.log(numbers) }
console.log("numbers before function is called " +numbers)
//refactor function code above using arrow function
const mutate = () => numbers.sort()
//call function mutate()
console.log("numbers after function is called " +numbers)
if I call the variable "numbers" after the function you will notice that the result is [2,5,7].
A const declaration alone doesn't really protect your data from mutation. To ensure your data doesn't change, JavaScript provides a function Object.freeze to prevent data mutation.
let movies = { name:"Dune", review: "Awesome" };
Object.freeze(movies); //note : make sure to use uppercase "O" in Object.freeze()
movies.review = "bad"; movies.newProp = "Release_year"; console.log(movies);
If you attempt to change the movie's object, it will be rejected n