Declaring Variables in JavaScript

Joy Evans
Joy Evans
Mar 26 · 3 min read
Photo by Erda Estremera on Unsplash

While learning JavaScript during Module 3 at Flatiron, I had some trouble working with variables. More specifically, I had difficulty with the three different ways to declare and assign a variable: var, let, and 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.

So before I talk more about the three different ways to declare a variable, we should discuss what not to do when naming that variable. I want to say that JavaScript doesn’t ask for much, but sometimes it may not work with what you have planned, but that's life. Anyway, these rules are quite simple, and it goes as follows:

So let’s dive in by reviewing the original way we use to declare a variable: var

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):

var name;
/* this creates a variable with the name 'name' */

or at the same time (as shown below):

var name = "Joy";
//=> undefined

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):

name;
//=> "Joy"

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: let and const .

Unlike var, both let and 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 .

let name = "Joy"
//=> undefined

Now let’s say that you don’t want to reassign or even redeclare a variable. I would recommend using theconst declaration hands down.

const name = "Joy"
//=> undefined

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.

const name;
//=> Uncaught SyntaxError: Missing initializer in const declaration

const name = "Joy";
//=> undefined

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 var , let and const works in case you prefer a visual aid. Happy coding!!!

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Joy Evans

Written by

Joy Evans

Currently a Software Engineer student at Flatiron School in Houston, TX.

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store