"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Declaration using const vs freeze in Javascript

Declaration using const vs freeze in Javascript

Published on 2024-08-18
Browse:237

Declaration using const vs freeze in Javascript

Javascript const

Using const we can still modify the contents of a Javascript objects but the reference to this object will be immutable.

const product = {name: "Sugar", weight: "1 kg"};
product.name = "Some New Name";

console.log(product);
{
  name: "Some New Name",
  weight: "1 kg"
}

Javascript freeze

Using freeze is preferred in cases where we do not want to modify the contents of an object.

const product = {name: "Sugar", weight: "1 kg"};
Object.freeze(product);
product.name = "Some New Name";

console.log(product);
{
  name: "Sugar",
  weight: "1 kg"
}
Release Statement This article is reproduced at: https://dev.to/alishgiri/declaration-using-const-vs-freeze-in-javascript-5ak4?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3