When Should You Use “var”, “let” and “const” In JavaScript?

Gravit
3 min readJan 18, 2023

--

Photo by Markus Spiske on Unsplash

Introduction

In programming, variables are fundamental to the way we work. They let us store values and manipulate them any way we want. These values are stored in variables and can hold any type of data that you want like numbers, strings etc. So, what is the difference between var and let in JavaScript? Well, both enable us to declare variables which we can later assign values to. The difference is that let is block-scoped while var is function-scoped; this means that a variable declared with let will only be visible inside the {} it was declared in whereas one declared with var will be visible anywhere inside the function it was declared.

So, what is the difference between var and let in JavaScript? Well, both enable us to declare variables which we can later assign values to. The difference is that let is block-scoped and var is function-scoped; this means that a variable declared with let is only visible inside the {} it was declared in whereas a variable declared with var is visible anywhere inside the function it was declared.

So what is the difference between var and let in JavaScript? Well, both enable us to declare variables which we can later assign values to. The difference is that let is block-scoped and var is function-scoped; this means that a variable declared with let is only visible inside the {} it was declared in whereas a variable declared with var is visible anywhere inside the function it was declared.

A lot of people use var because they think it’s easier to understand but there are some cases where using let can make your code clearer:

  • If you’re using an object literal (e.g., { x: 100 }) then you should use let instead of var because they’re not interchangeable — if x has already been set elsewhere then trying to set its value will throw an error since it already exists!

const has a similar purpose as let but it’s used for immutable values (means that once a value has been assigned to the variable, it cannot be changed). Additionally, when you want to use const you need to set an initial value for it.

Oops! Click Regenerate Content below to try generating this section again.

“What does immutable mean?”, I hear you ask… For example, let’s say that you were asked to write a program for calculating how much food your dog ate during his lifespan. In this case, the dog’s age (in human years) would be mutable since it changes every year but the number of food bowls he’d need per day would be immutable — since he would always eat 2 bowls of food per day during his life.

“What does immutable mean?”, I hear you ask… For example, let’s say that you were asked to write a program for calculating how much food your dog ate during his lifespan. In this case, the dog’s age (in human years) would be mutable since it changes every year but the number of food bowls he’d need per day would be immutable — since he would always eat 2 bowls of food per day during his life.

Immutable variables are useful for values that don’t change, like the number of food bowls per day your dog eats or the number of times they’ve barked at night while sleeping in their bed (you know those kinds of things).

Conclusion

Variables come in handy when you need to store data. They’re also known as properties of an object and they have a specific scope which is defined by the block they are declared in. So, if you want to access these variables outside their block then we need to use an arrow operator (->) in order make sure we don’t get an error message saying that there isn’t any such variable or object at that point in time.

--

--

Gravit

I am Garvit Singh, a computer science student interested in web development and technology, and I write blogs related to coding and technology.