Home > Web Front-end > JS Tutorial > Declaration using const vs freeze in Javascript

Declaration using const vs freeze in Javascript

WBOY
Release: 2024-08-12 18:33:21
Original
487 people have browsed it

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);
Copy after login
{
  name: "Some New Name",
  weight: "1 kg"
}
Copy after login

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);
Copy after login
{
  name: "Sugar",
  weight: "1 kg"
}
Copy after login

The above is the detailed content of Declaration using const vs freeze in Javascript. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template