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. Running Everything as Root One of the biggest beginner errors. Many new users log…
A keylogger is a type of surveillance software or hardware that records every keystroke made…
In JavaScript, it’s commonly used for: Recursive functions (like Fibonacci) Heavy calculations Repeated API/data processing…
For years, responsive design has depended almost entirely on media queries. We ask questions like: “If…
1. What is Task Scheduling? Task scheduling is the process of automatically running commands, scripts,…
Here’s a comprehensive, clear differentiation between a Website and a Web App, from purpose all the…