DEV Community

Discussion on: Why you shouldn't reassign values in JavaScript

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Just noticed you may want to rewrite this bit:

I recommend always create variables with the const keyword. This is because variables created with a const keyword cannot be reassigned. You'll get an error if you try to assign a new value to them.

The const keyword creates constants, not variables.

Collapse
 
shalvah profile image
Shalvah • Edited

They're not really "constants" in a strict sense because, even though you can't reassign, you can modify the referenced value (if it's an object). For instance:

const x = {a: 1};

// this will fail
x = {a: 2};

// but this will pass
x.a = 2;