DEV Community

let & const

Visakh Vijayan on January 16, 2019

ES6 introduced two new alternatives to var keyword namely let and const. var allows you to define the same variable over and over again without a...
Collapse
 
ceckenrode profile image
Christian Eckenrode

Awesome write up!

Just want to point out that if you have an array or an object declared with const, you can still modify them since they’ll still be pointing to the same value in memory. Example:

This is okay!

const person = {}
person.name = Christian

const hobbies = [playing guitar]
hobbies.push(drawing)

But doing either of these will result in an error

const person = {}
person = { name: Visakh }

const hobbies = []
hobbies = [coding]

So you can modify arrays an objects, but you can reassign them altogether (by resetting their value with the equals sign)

const in JavaScript doesn’t mean constant value, it means constant reference. See here.

You’re 100% correct when it comes to other primitive types though 🙂

Collapse
 
qzsg profile image
Adrian

If an array or an object is assigned to a const, note that it's contents can still be modified