A variable is a named container used to store data that your program can use, modify, and reference later.
Think of it as a label attached to a value in memory.
let age = 25; Here:
age → variable name25 → value stored in memoryLearn software development with Codeflare
Variables allow you to:
Without variables, programs would be static and useless.
JavaScript provides three keywords for declaring variables:
var (old, mostly avoided)let (modern, recommended)const (modern, preferred when values shouldn’t change)var — The Old Way (Avoid in Modern Code)var name = "Luke"; var:undefined)var:if (true) {
var x = 10;
}
console.log(x); // 10 (unexpected!) ⚠️ This behavior caused many bugs, which is why let and const were introduced.
let — Modern, Flexible Variableslet score = 50;
score = 60; let:if (true) {
let y = 20;
}
console.log(y); // ❌ ReferenceError ✔️ Use let when the value will change.
const — Constant Variables (Most Preferred)const pi = 3.14; const:const country = "Nigeria";
country = "Ghana"; // ❌ Error ✔️ Use const by default.
const with Objects and Arrays (Important!)const prevents reassignment, not mutation.
const user = { name: "Luke", age: 30 };
user.age = 31; // ✅ Allowed const numbers = [1, 2, 3];
numbers.push(4); // ✅ Allowed ❌ But reassignment is not allowed:
numbers = [5, 6]; // Error Accessible everywhere.
let globalVar = "I am global"; Accessible only inside the function.
function test() {
let localVar = "Local";
} Accessible only inside {} blocks (if, for, etc.)
if (true) {
let blockVar = "Inside block";
} Hoisting means JavaScript moves declarations to the top during compilation.
var hoisting:console.log(a); // undefined
var a = 5; let and const hoisting:console.log(b); // ❌ ReferenceError
let b = 5; This happens because of the Temporal Dead Zone (TDZ).
let firstName;
let _count;
let $price; let 1name; // ❌
let first-name; // ❌ let totalAmount;
let isLoggedIn; JavaScript is dynamically typed, meaning variables can change types.
let value = 10;
value = "Ten";
value = true; ⚠️ This is powerful but can cause bugs if not careful.
let x; x = 100; let x = 100; ✔️ Prefer const
✔️ Use let when reassignment is needed
❌ Avoid var
✔️ Keep scope as small as possible
✔️ Name variables clearly
var unintentionallyvar count = 0; let or consttotal = 50; // ❌ Creates a global variable constconst x = 5;
x = 6; // ❌ Error | Keyword | Scope | Reassign | Redeclare | Recommended |
|---|---|---|---|---|
| var | Function | ✅ | ✅ | ❌ |
| let | Block | ✅ | ❌ | ✅ |
| const | Block | ❌ | ❌ | ✅✅ |
Use
constby default.
Useletonly when the value must change.
Avoidvar.
Latest tech news and coding tips.
Almost everyone starts learning JavaScript with the wrong expectations. Let's fix them. Download the Codeflare…
Phaser JS is a powerful, open-source HTML5 game development framework used for creating 2D games that…
JavaScript / Node.js Authentication Libraries 1. Passport.js One of the most popular authentication middleware libraries…
Every profession comes with its own set of tools. A carpenter has a toolbox, a…
Every application that stores and manages data relies on a set of basic operations known…
PHP remains one of the most widely used server-side programming languages, powering platforms such as…