DEV Community

peter munyambu
peter munyambu

Posted on

Understanding Variables in JavaScript: var, let, and const Explained for Beginners

published: true
description:
A beginner-friendly guide to understanding the differences between var, let, and const in JavaScript, with examples and use cases."
tags: javascript, beginners, webdev, programming

Introduction

When I started learning JavaScript, one of the first things I came across were the keywords var, let, and const. At first, they all seemed to do the same thing — they allowed me to create variables. But as I went deeper, I realized that there are big differences between them. In this post, I’ll explain what each one does, when to use them, and why choosing the right one is important for writing clean and bug-free code.

What Are Variables?

In programming, variables are like containers that store data. You give them a name, and they hold a value that you can use or change later.

Here’s a simple example:

js
let name = "Sam";
const age = 25;
var job = "Engineer";

All of these lines declare a variable, but the way JavaScript treats them under the hood is different.

var vs let vs const

  1. var – The Old Way

var is the original way of declaring variables in JavaScript. It works, but it has some problems because it doesn’t have block scope.

js
if (true) {
var message = "Hello from inside!";
}
console.log(message); // ✅ Still works!

That means message is available outside the if block, which can lead to unexpected bugs.

  1. let – The Modern Way

let was introduced in ES6 (2015) and is now the recommended way to declare variables that can change later. It has block scope, which is much safer.

js
if (true) {
let greeting = "Hi!";
}
console.log(greeting); // ❌ Error: greeting is not defined

This is good because it keeps your variables inside the block they belong to.

  1. const – For Values That Don’t Change

const is also block-scoped like let, but you can’t reassign it once it has a value.

js
const planet = "Earth";
planet = "Mars"; // ❌ Error: Assignment to constant variable

It’s perfect for things you know won’t change, like configuration values or fixed data.

When Should You Use Which?

Keyword Can Reassign? Block Scoped? Common Use
var ✅ Yes ❌ No Avoid if possible (old JS)
let ✅ Yes ✅ Yes Use when the value may change
const ❌ No ✅ Yes Use when the value should stay constant

Common Mistakes to Avoid

  1. Using var in modern code – it can cause confusing behavior because it ignores block scope.
  2. Using const for variables you plan to change – this will throw errors if you try to reassign.
  3. Not initializing a variable – always give a variable a value when you declare it, especially with const.

My Takeaway

At first, I was using var for everything. But after learning how let and const help protect my code from bugs, I now mostly use const unless I need to change the value later, in which case I use let. I avoid var entirely unless I’m working in an older codebase.

Resources

MDN Web Docs: let

MDN Web Docs: const

FreeCodeCamp: JavaScript Variables

Conclusion

Understanding the difference between var, let, and const is a small but powerful step in learning JavaScript. It helps you write cleaner, safer code and avoid tricky bugs. As a beginner, I’ve found that focusing on let and const gives me better control over how my variables behave.

Thanks for reading! If you’re learning JavaScript too, I’d love to hear what helped you understand variables better.

Let me know if you’d like a second blog post on another concept (like functions, arrays, or DOM manipulation) or if you want help designing a custom blog using HTML/CSS/JS!

Top comments (0)