Learn how to store, update, and manage data in JavaScript using
var, let, and const — the building
blocks of every program you'll ever write.
In JavaScript, a variable is a named container used to store
data values that can be referenced and manipulated during program execution.
Variables are declared using var, let, or
const.
Every piece of data your program works with — a user's name, a score, a price — lives inside a variable. Naming it clearly makes your code readable and maintainable.
Think of a variable like a labelled box:
You can store, update, and use data inside this box at any point in your program.
var name = "Aman";
let age = 22;
const country = "India";
console.log(name);
console.log(age);
console.log(country);
var → old way of declaring variables (function-scoped, avoid in modern code)let → modern declaration whose value can be changed laterconst → fixed value that cannot be reassigned after declarationlet score; // Declaration — variable created, value is undefined
score = 10; // Initialization — first value assigned
score = 20; // Reassignment — value updated
console.log(score);
let / var)const pi = 3.14;
console.log(pi);
// Trying to reassign const — this throws an error:
try {
pi = 3.14159;
} catch (e) {
console.log("Error:", e.message);
}
const locks the binding — once assigned, the variable cannot point to a new valueTypeError at runtimeImagine managing your wallet and identity documents:
Variables power everything on a real page. Click ▶ Preview, then press the button inside to see variables displayed live in the browser.
<!DOCTYPE html>
<html>
<head><title>Variables Demo</title></head>
<body style="font-family:sans-serif;padding:24px;background:#f8f9fa">
<h2>JavaScript Variables</h2>
<button onclick="showVars()"
style="padding:8px 20px;background:#f7df1e;border:none;
border-radius:6px;cursor:pointer;font-weight:700;font-size:1rem">
Click Me
</button>
<div id="output" style="margin-top:16px;line-height:2"></div>
<script>
function showVars() {
const userName = "Aman";
let age = 22;
let wallet = 1000;
wallet -= 300; // spent ₹300
document.getElementById("output").innerHTML =
"<b>Name:</b> " + userName + "<br>" +
"<b>Age:</b> " + age + "<br>" +
"<b>Wallet:</b> ₹" + wallet;
}
</script>
</body>
</html>
// Replace values with your own
let name = "Your Name";
let city = "Your City";
name and city using letconsole.log()let marks = 50;
marks by 20 using +=let to const — what happens?const declares a variable whose binding cannot be reassigned. Use it whenever you know the value should never change.
let allows the value to be updated at any time. const locks the binding after the first assignment.
const x = 10;
x = 20;
const throws a TypeError at runtime. The first assignment is the only one allowed.
Prefer const by default — only switch to let when you know the value will change. This prevents accidental reassignment bugs.
const taxRate = 0.18; // never changes
let cartTotal = 500; // will be updated
cartTotal += 200;
console.log(cartTotal);
console.log(taxRate);
Use meaningful variable names — code is read far more often than it is written:
let userAge = 25; // ✓ clear intent
let x = 25; // ✗ what is x?
const maxRetries = 3; // ✓ self-documenting
const n = 3; // ✗ meaningless
console.log(userAge, maxRetries);
Avoid var in modern JavaScript. It is function-scoped (not block-scoped), which causes subtle bugs in loops and conditionals that let and const prevent entirely.
Use camelCase for variable names — it is the JavaScript convention:
let firstName = "Priya"; // ✓ camelCase
let first_name = "Priya"; // ✗ snake_case (Python style)
let FirstName = "Priya"; // ✗ PascalCase (reserved for classes)
console.log(firstName);
Predict both outputs before running — think about how values are copied between variables.
let a = 10;
let b = a;
b = 20;
console.log(a);
console.log(b);
// Step 1: a = 10
// a → [10]
// Step 2: b = a
// b gets a COPY of the value — not a reference
// b → [10] (a is still [10])
// Step 3: b = 20
// only b changes — a is unaffected
// b → [20] a → [10]
// Output:
// 10 ← a is unchanged
// 20 ← b was updated independently
// Key takeaway: primitive values (numbers, strings, booleans)
// are copied by VALUE — changing b never affects a.