const. I understood that one of the best ways to describe a variable is to look at it as if it's a box. That box can contain an item you ordered online, a pair of your favorite shoes you only wear when going out, or even a cherished memory from your childhood. Regardless of the type of object stored in that box, hopefully, you can access that box whenever you need it. The good news is you would assign that box a name so that you can access it anytime.
- start every variable name with a lowercase letter
- stay away from reserved words and future reserved words
So let’s dive in by reviewing the original way we use to declare a variable:
Up until 2015,
var is the only way to declare variables, essentially making it the only child.
var declares a function-scope or globally scoped variable. So when using it you have the option of declaring and assigning the variable in two separate steps (as shown below):
/* this creates a variable with the name 'name' *///=> undefinedname = "Joy"
/* this give the variable the string value 'Joy' *///=> "Joy"
or at the same time (as shown below):
var name = "Joy";
Both methods are standard, but regardless of which you decide to use, you only have to refer to its name when you’re ready to retrieve that variable (as shown below):
Now once that variable has a name, you do have the option to redeclare it as many times you desire without triggering an error. That can be both a blessing and a curse because you run the risk of unintentionally declaring that variable twice. It can lead potentially to significant problems down the road; however, ES2015 introduced two new ways to create variables:
const statements will throw an error if you try to declare a variable twice, but they have their differences. Just like
var , you can reassign a variable by using
let name = "Joy"
//=> undefinedname = "Hope"
Now let’s say that you don’t want to reassign or even redeclare a variable. I would recommend using the
const declaration hands down.
const name = "Joy"
//=> undefinedname = "Hope"
//=> Uncaught TypeError: Assignment to constant variable.
In addition to not being able to reassign or redeclare a variable, you would also have to assign the value right away. Another fun fact about using
const is that you can declare it with uppercase or lowercase letters. It's more common to use uppercase, but that’s entirely up to you as a developer.
//=> Uncaught SyntaxError: Missing initializer in const declaration
const name = "Joy";
To recap, stay away from
var , use
let when you know that value will change, and use
const if you know that the value won’t change. Of course, I can’t end this topic and not provide any helpful resources, and this table that offers helpful hints of how
const works in case you prefer a visual aid. Happy coding!!!
The var statement declares a function-scoped or globally-scoped variable, optionally initializing it to a value…
The let statement declares a block-scoped local variable, optionally initializing it to a value. The names of the…