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.
1. What Is a Queue? A Queue is a linear data structure that follows the principle: FIFO – First…
Angular is a full-featured frontend framework built by Google for creating large, maintainable, and high-performance web applications.…
What Is Responsive Web Design? Responsive Web Design (RWD) is an approach to building websites…
The Geolocation API allows a web application to access a user’s geographical location (latitude, longitude, and more), with…
1. What Is the Golden Ratio? The Golden Ratio, represented by the Greek letter φ (phi), is…
In CSS, combinators define relationships between selectors. Instead of selecting elements individually, combinators allow you to target elements based…