Streaming on Twitch twice a week

Freezing objects for immutability


In JavaScript, const doesn’t mean a value is constant and can never be changed. To make an object immutable, we have to freeze it.

// the word `const` makes it sound like this object can never be modified
const dog = { name: 'Coop' }
// dog → { name: 'Coop' }

// we can still change properties of an object defined with `const` = 'Cooper'
// dog → { name: 'Cooper' }

// we can also add new properties to it
dog.age = 3
// dog → { name: 'Cooper', age: 3 }

// the only thing we cannot do is assign an entirely different object
dog = { name: 'Emil' }  // TypeError (cannot assign to readonly property)

// to prevent ALL changes to an object, we have to freeze it
const cat = Object.freeze({ name: 'Milly' })
// cat → { name: 'Milly' }

// frozen objects can’t be changed (these steps throw errors in strict mode) = 'Lilly'
cat.age = 9
// cat → { name: 'Milly' }

More fire tips

Namespacing styled-components

To improve the readability of React components, we can import the styled-components they use from another file under a ui-namespace.

Read fire tip

Read all fire tips →