Interview Series #1: Var, let, and const
Published: 2021-07-23 17:22:07

Since graduating from Flatiron's Software Engineering bootcamp, I've had my first round of interviews. These have ranged from purely behavioral/cultural interviews to technical interviews to "technicalish" interviews. I've decided that as I go through this process, I'm going to attempt to blog about the most salient things I've experienced. 

You could encounter a wide variety of scenarios in your interview process from being asked to solve a coding challenge, live coding a simple application, or simply answering programming questions. In either case, remembering the basics is crucial. 

This blog post is going to focus on a very foundational concept in JavaScript programming, but it is a question you could very well be asked in an interview. That question is this: ***what is the difference between var, let, and const***.

Var, let and const refer to variable declaration in JavaScript. In other words, the first time you introduce a variable, you ***declare*** it with a keyword. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5jonkjbmkz81ank6rxfu.PNG)

When I first learned JavaScript, I was only introduced to the ***var*** keyword. Let and const are new additions from ES6. There are several reasons that let and const are now considered preferable to var, but one big reason is that you can RE-DECLARE variables using the var keyword without causing an error. For example: 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ptjk0w79te5vywl4ef8q.PNG)

As programmers we are used to REASSIGNING a variable's value over the course of a program, but allowing for multiple variable declarations of the same name can lead to unseen bugs and generally less coherent, readable code. 

Using the same basic example but substituting *var* for *let* returns an error:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/thwofsi25h6z8yjzbhla.PNG)

## Let and Const Explained

At this point, let and const have supplanted var as the preferred variable declaration keywords. However, there are important things to know about each keyword.

Most significantly, if you want to be able to reassign the value of a variable over the course of a program, you must use the *let* keyword. If you are declaring a variable whose value you want to remain CONSTANT (get it), you will use the *const* keyword. If you attempt to reassign a variable declared with const, you will get an error:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bpj28w39vl2uv2qpmo5p.PNG)

##Let's Talk About Scope

There is an importance difference between *var*, *let*, and *const* in regards to scope. As you probably know, var is global or function-scoped. So, we're used to being able to access a variable declared with var anywhere in the function it is declared like so:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/39w0eub06ajlwcu7gkjk.PNG)

But *let* and *const* are **block scoped**, meaning they are only accessible within the code block they are declared in. Look at this example using the let keyword:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2im0ouz0t2kdedc4fev0.PNG)