Using ‘const’ in JavaScript
There’s a few different ways to declare variables in JavaScript. Originally var was the way to create a variable, but since ES6 ‘let’ and ‘const’ have been introduced. To consistently keep a variable, ‘const’, is the way to do it.
Get aaron brady’s stories in your inbox
Join Medium for free to get updates from this writer.
When a variable is declared with const instead of var or let, it cannot be reassigned.
const newVariable = 'This name'Attempting to redeclare the variable will result in the following:
const newVariable = 'This name'
const newVariable = 'cannot be changed'
// 'newVariable' has already been declaredIf you are looking to redeclare your variables in JS land, then you may want to use ‘var’ or ‘let’. Then you have to be cautious of hoisting, which is a whole different subject. If you refrain from using ‘var’ since ES6, then you do not have to worry too much hoisting. Using var at this point is obsolete. Sticking to using const or let is the best way to declare you variables.




