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.
Backpressure is a flow-control mechanism that prevents a fast data producer from overwhelming a slower data…
JavaScript collections are data structures used to store, organize, retrieve, and manipulate groups of values. Choosing…
Context poisoning is a technique where an attacker deliberately places misleading, malicious, or irrelevant information into…
React makes building user interfaces easier, but certain errors appear repeatedly, especially when working with…
The C programming language is a foundational, general-purpose computer programming language created by Dennis Ritchie at Bell Labs in 1972. Originally developed to…
What Is Homebrew? Homebrew is a package manager for macOS and Linux that makes it easy to…