employer cover photo
employer logo
employer logo

i-exceed technology solutions

Ist dies Ihr Unternehmen?

Frage im Vorstellungsgespräch bei i-exceed technology solutions

Explain the difference between Object.freeze() vs const?

Antwort im Vorstellungsgespräch

Anonym

14. Juli 2022

.const applies to bindings ("variables"). It creates an immutable binding, i.e. you cannot assign a new value to the binding. const person = { name: "Leonardo" }; let animal = { species: "snake" }; person = animal; // ERROR "person" is read-only Object.freeze works on values, and more specifically, object values. It makes an object immutable, i.e. you cannot change its properties. let person = { name: "Leonardo" }; let animal = { species: "snake" }; Object.freeze(person); person.name = "Lima"; //TypeError: Cannot assign to read only property 'name' of object console.log(person);