Understanding JavaScript Variables: var, let, and const Explained

Anderson Servat
3 min readJan 28, 2024

--

If you’re just starting your journey into the world of JavaScript, you’ve likely encountered different ways to declare variables: var, let, and const. These three keywords may seem confusing at first, but they play crucial roles in how you manage and use your variables in JavaScript. Let's break down the differences in a way that's easy to grasp.

1. var: The Old School Player

Imagine var as the veteran of the group, having been around for a long time. When you use var to declare a variable, it is accessible throughout the entire function where it's declared. If you declare it outside any function, it becomes a global variable accessible anywhere.

One quirky thing about var is hoisting. This means that even if you use a variable before declaring it in your code, JavaScript will make it work by moving the declaration to the top during its initial phase. However, the assigned value won't be hoisted – just the declaration.

function example() {
if (true) {
var x = 10;
}
console.log(x); // Outputs 10, thanks to hoisting
}

Also, var allows you to redeclare the same variable within the same scope, which can lead to unexpected behavior.

2. let: The Block-Scoped Hero

Now, enter let. Think of it as the hero that prefers working within specific blocks of code, making it more predictable. Variables declared with let are only accessible within the block (the area enclosed by curly braces) where they are declared.

Unlike var, let doesn't hoist the variable to the top of its scope during the compilation phase. It's hoisted, but the actual initialization only happens when the code execution reaches the declaration.

function example() {
if (true) {
let y = 20;
console.log(y); // Outputs 20 without any hoisting magic
}
console.log(y); // Error: y is not defined outside the block
}

Another noteworthy point is that let doesn't allow you to redeclare the same variable within the same scope, avoiding potential confusion.

3. const: The Constant Guardian

Meet const, the guardian of constants. When you declare a variable with const, you're stating that its value won't change. Like let, it's block-scoped, limiting its accessibility to the block where it's defined.

However, the rigidity of const is its strength. Once a value is assigned to a const variable, you can't reassign it. It's a great choice when you want to ensure that a variable remains constant throughout your cod

const z = 30;
// z = 40; // Error: You can't change the value of a const variable

const obj = { key: 'value' };
obj.key = 'new value'; // Valid, as we are modifying a property, not reassigning the entire variable

Understanding these three keywords — var, let, and const – gives you the power to control your variables in JavaScript. As a beginner, focus on using let for regular variables that might change and const for those values you want to keep constant. Leave var in the past unless you're dealing with legacy code.

Happy coding, and welcome to the exciting world of JavaScript!

--

--

Anderson Servat
Anderson Servat

Written by Anderson Servat

Attorney licensed in Brazil, ENTP, dyslexic. Paralegal, Texas Realtor. Back-end Developer. Stockbroker.

No responses yet