Var, Let, Const in JavaScript
Tags: flatiron, javascript, softwareengineering, scope
Published: 2023-07-23 11:29:31

Hi! My name is Ramon Antonio Garcia and I am currently enrolled in the Front-End Software Engineering bootcamp with Flatiron School based in New York City and Denver. However, due to location and time constraints, I am currently enrolled in the online program over the course of 10 months for this cohort.

In this blog, I would like to talk about variable declaration in JavaScript and how the choice of our declaration affects the code we write and deploy. In other words, we must always choose carefully to avoid unwanted and unexpected errors down the line.

## VAR
When declaring a VAR variable, it can either be a globally-scoped or function-scoped variable. You may if you want initialize the variable to a value upon declaration.

A typical problem that we encounter with a VAR variable is that it can be declared more than once in different scopes of the programming code blocks. So not only can you re-initialize but the variable itself can be redeclared. This practice could be prone to user errors and bugs in the system.

Due to all of the issues that developers would encounter with VAR, two new species were introduced back in 2015 with the new ES6. These new keywords are LET and CONST.  Please note that I am using CAPS to refer to these three keywords (VAR, LET, CONST) but they do not have to be capitalized in the coding.

## LET
With the new introduction of the LET keyword, and while VAR still exist for legacy code, LET has pretty much taken over when it comes down to variable declaration in JavaScript.

LET is function-scoped (block-scoped). That is if a variable is declared within a function, it cannot be accessed outside of that function (global-scoped) anywhere within that window.

Unlike VAR, a LET variable can me updated as many times as possible but it CANNOT be re-declared. That would essentially throw an error in the system.

## CONST
The CONST keyword was introduced to handle a new type of variable that neither VAR nor LET can do. It essentially stands out for CONSTANT.  While it is similar to LET variables that are block-scoped, that is they can only be accessed within that same block of code. CONST variables CANNOT be updated.

Similarly to LET variables, a CONST variable can also NOT be re-declared. This would essentially throw an error in the system. Since these hold constant values, it MUST be initialized upon variable declaration.

One tricky thing about CONST variables is that if we declare an OBJECT, the properties of that OBJECT can be updated and it would allow you to do so. So in that sense, it would behave similarly than a LET variable.

I hope you enjoy this short initial blog. I am really not used to blogging but I will make it a thing moving forward.

## The Author
I am also taking advantage of this opportunity to say a little bit more about myself. I used to work as a developer (mainly backend) over 15-20 years ago but then life happened to me and I stayed away from development for slightly over 10 years of my life. As a way to get back into coding, I decided to apply to this online bootcamp. After all of my research, I decided Flatiron School would be the best option for me. I am happy I made it in!