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.
Why borrowing code is a skill—when you understand what you're copying. For years, "copy-paste developer"…
Interactive forms rarely keep every field active all the time. Sometimes an input should only…
A modern JavaScript API for working with dates, times, time zones, and calendars without the…
Understanding What Happens Behind the Scenes Node.js looks simple from the outside—you write JavaScript, call…
The need for absolute certainty is the greatest disease the engineering mind faces. The moment…
The software industry is one of the most competitive places to build a career. Every…