Login

Sign Up

JavaScript Variables: Var, Let & Const
Priyanshu Sharma

Posted on Jun 25, 2025 | Frontend

JavaScript Variables: Var, Let & Const

Hey curious coder! ๐Ÿ‘‹
So youโ€™ve started exploring JavaScript and bumped into these three oddballs:
var, let, and const.

They all claim to help you store stuff like numbers, names, or even your deepest coding secrets ๐Ÿ•ต๏ธโ€โ™‚๏ธ โ€” but how do they work? And what's the difference?

Letโ€™s break it down like weโ€™re at a coding party. ๐ŸŽˆ


๐ŸŽ What is a Variable?

In simple words, a variable is like a box with a label.
You put something inside โ€” like a name or number โ€” and can use or change it later.

Example:

let name = "Ben";
console.log(name); // Output: Ben

Here, name is the label, and "Ben" is the gift inside the box. ๐ŸŽ


Meet the Three: var, let, and const

They all create variables, but with different rules.
Think of them like siblings with different personalities at a dinner table:

var โ€“ The Old Grandpa

var age = 25;
  • Used in the old days of JavaScript
  • Has function scope (only cares about functions)
  • Can be re-declared and updated
  • Sometimes does weird things because of hoisting (explained below)

๐Ÿ˜… Might cause confusion in big codebases โ€” so not recommended anymore unless you know what you're doing.

let โ€“ The Cool and Modern One

let city = "Delhi";
  • Introduced in ES6 (a modern update of JS)
  • Has block scope (respects {} like a true gentleman)
  • Can be updated, but not re-declared in the same block

๐Ÿ‘ Use this when your variable might change later (like user input, score, etc.)

const โ€“ The Rule Follower

const pi = 3.14;
  • Also introduced in ES6
  • Has block scope
  • Cannot be updated or re-declared โ€” it's constant ๐Ÿ”’

Use const when your value should never change (like pi, app settings, etc.)


๐Ÿคฏ Wait, Whatโ€™s โ€œScopeโ€?

Scope decides where a variable can be used.

  • Function Scope (like var): Variable lives inside a function
  • Block Scope (like let and const): Variable lives inside {} like if, for, etc.

Example:

if (true) {
  let food = "Pizza";
}
console.log(food); // โŒ Error! `food` is out of scope

๐Ÿช„ Bonus: Hoisting (The Magic Trick You Didn't Ask For)

var can be used before itโ€™s declared โ€” kinda like black magic. ๐Ÿง™

console.log(x); // undefined ๐Ÿ˜ถ
var x = 10;

But with let and const, this wonโ€™t work:

console.log(y); // โŒ ReferenceError
let y = 20;

That's why var can be risky for beginners.


๐Ÿ So, What Should You Use?

โœจ Use let when your variable might change.
๐Ÿ”’ Use const when it shouldn't.
๐Ÿ›‘ Avoid var unless you have a specific reason or are dealing with legacy code.


๐Ÿง  Final Thoughts

Variables are the building blocks of any programming language.
Think of them as your coding containers โ€” and now you know which container to use when!
Keep experimenting, keep building, and donโ€™t worry if it breaks โ€” itโ€™s how we learn! ๐Ÿ’ช

Happy Coding!

6 Reactions

0 Bookmarks

Read next

Priyanshu Sharma

Priyanshu Sharma

Dec 14, 24

3 min read

|

How to Setup & Install VS Code: A Beginnerโ€™s Guide

Priyanshu Sharma

Priyanshu Sharma

Dec 17, 24

6 min read

|

Essential VS Code Extensions for Developers