SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
JavaScript
1. JavaScript Tutorial for Beginners — Learn JS from Scratch 2. Where to Put JavaScript Code — Head, Body or .js File 3. JavaScript Output Methods — alert, innerHTML, console.log 4. JavaScript Syntax Explained — Statements & Rules 5. JavaScript Comments — How to Write // and /* */ 6. JavaScript Variables — var, let, const Explained 7. JavaScript Operators — Arithmetic, Comparison, Logical 8. JavaScript Data Types — string, number, object, array 9. JavaScript Functions Tutorial — Parameters & Return Values 10. JavaScript Objects — Properties, Methods & this Keyword 11. JavaScript Scope — Global, Local & Block Scope Explained 12. JavaScript Events — addEventListener with Examples 13. JavaScript Strings — Methods, slice, replace & Templates 14. JavaScript Numbers — parseInt, toFixed, NaN Explained 15. JavaScript Arrays — Create, Read, push, pop & More 16. JavaScript Array Methods — map, filter, forEach, find 17. JavaScript Date Object — new Date(), getFullYear & More 18. JavaScript Math Object — random, round, floor, ceil 19. JavaScript if else — Conditions with Examples 20. JavaScript switch Statement — case, break, default 21. JavaScript for Loop — for, for...of, for...in Examples 22. JavaScript while Loop — while & do...while Explained 23. JavaScript Type Conversion — Number, String, Boolean 24. JavaScript try catch — Error Handling with finally 25. JavaScript JSON — stringify & parse with Examples 26. JavaScript Arrow Functions — => Syntax & this 27. JavaScript Classes — Constructor & Objects Explained 28. JavaScript Promises & async/await Explained Simply 29. JavaScript DOM Tutorial — Find & Change Page Elements 30. JavaScript Form Validation Tutorial with Examples 31. JavaScript Fetch API Tutorial — fetch() with Examples 32. JavaScript localStorage — setItem, getItem Explained 33. JavaScript Timers — setTimeout & setInterval Examples 34. JavaScript Regular Expressions — test() & match() Guide 35. JavaScript Debugging — console.log & Fixing Errors
Final Test — Certificate ₹10
Try this code live
Lesson 6 of 35

JavaScript Variables — var, let, const Explained

4 min read · Free · No login needed

A variable is a named box that holds a value. You put a number, a piece of text, or a true/false answer into the box, give the box a name, and use that name whenever you need the value again. Without variables, every program would have to repeat every number by hand — and changing one price would mean hunting through the whole program.

Diagram — see how it fits together
Diagram: JavaScript Variables

JavaScript has three ways to create a variable: var, let, and const. They look similar, but they behave differently. Modern code uses let and const almost always; var is the old way, kept around so old programs still work.

let — a box you can change

let creates a variable whose value can change later. The number of cups in an order, the marks a student scores, the name typed into a form — all of these change, so they belong in let boxes.

let cups = 2;
cups = 5; // the order grew — the same box now holds 5

const — a box you cannot change

const creates a variable whose value can never change after it is set. The price of a cup of chai, the pass marks for an exam, the name of your academy — fixed facts belong in const boxes. If you try to change a const later, JavaScript stops with an error. That is a feature, not a bug: it catches mistakes where you accidentally overwrite a value that should stay fixed.

const price = 10;
const passMarks = 33;
// price = 15;  // error! A const cannot be changed.

var — the old way

var was the only way to create variables before 2015. It still works, but it has a loose rule — a var declared inside a block can leak out of it — that causes confusing bugs. New code should use let and const. When you read old tutorials and see var, know that it means the same thing as let in most simple cases.

Naming rules — what a variable can be called

A variable name must start with a letter, an underscore _, or a dollar sign $. After that it can contain letters, digits, underscores, and dollar signs. It cannot contain spaces, and it cannot be a reserved word like let or function. The convention is camelCase: the first word lowercase, each following word starting with a capital letter — totalBill, studentName, chaiPrice. Good names describe the value: price is good, x tells nobody anything.

Note: A variable created with let or const cannot be used before the line that creates it. This is called the "temporal dead zone". A variable created with var can be used before its line, but it will hold the value undefined — another reason modern code prefers let and const.
Tip: A simple rule for choosing: start with const. If you later find you need to change the value, switch that one variable to let. This habit stops a whole class of accidents before they happen.

Common mistakes

  • Using a variable before creating it — alert(total); let total = 40; is an error with let and const.
  • Trying to change a const — that is the whole point of const, and JavaScript will stop you.
  • Using the same name twice with let in the same place — let price = 10; let price = 15; is an error. Assign again instead: price = 15;.
  • Naming a variable with a space or a number first — let chai price = 10; and let 2cups = 5; are both errors.

Remember

  • let — a variable you can change later.
  • const — a variable you can never change. Start here by default.
  • var — the old way. It works, but prefer let and const.
  • Names use camelCase: totalBill, studentName. No spaces, no numbers first.

Example explained

  • const price = 10; — the price of one cup never changes during the order, so it goes in a const box.
  • let cups = 3; — the number of cups can change (the customer might add more), so it goes in a let box.
  • let total = cups * price; — a third variable, calculated from the first two. Change cups or price, and the total changes with them.
  • document.getElementById("cups").value — reads the number the visitor typed into the input box. The input's value is always text, so Number(...) turns it into a real number.
  • "Total: ₹" + total — the + joins the text with the number, producing one message for the page.

Example

Example
<!DOCTYPE html>
<html>
<head>
<title>Chai Bill with Variables</title>
<style>
body { background-color: #fff8e1; font-family: Verdana, sans-serif; text-align: center; }
h1 { color: #b45309; }
input { padding: 10px; font-size: 16px; width: 80px; text-align: center; }
button { background-color: #e13611; color: white; border: none; padding: 12px 24px; font-size: 16px; cursor: pointer; }
#bill { color: green; font-weight: bold; font-size: 18px; }
</style>
</head>
<body>

<h1>Chai Bill with Variables</h1>
<p>Masala Chai costs ₹10 per cup. How many cups?</p>
<input id="cups" type="number" value="3">
<button onclick="showBill()">Calculate bill</button>
<p id="bill">Enter the cups and click the button.</p>

<script>
function showBill() {
  const price = 10;
  let cups = Number(document.getElementById("cups").value);
  let total = cups * price;
  document.getElementById("bill").textContent = "Total: ₹" + total;
}
</script>

</body>
</html>

Output

Output — how it looks in the browser
Output of JavaScript Variables — var, let, const Explained

Try it yourself — live editor

Change the code below and press Run to see the result instantly. No file to save, no login needed.

Result

Frequently asked questions

The questions learners ask most about this lesson — read the question, then open it to see the answer.

1. When should I use let and when const?

Use let for values that change later, like cups in an order, and const for fixed facts like the price of a cup of chai.

2. What happens if I try to change a const?

JavaScript stops with an error, which is a feature: it catches mistakes where a fixed value is accidentally overwritten.

3. Is var still used in modern code?

var is the old way and still works, but modern code uses let and const almost always.

Why learn this yourself — and not just ask AI?

AI can write code for you, but it cannot think for you. When you learn HTML yourself, your brain builds the real skill: you understand why a page looks the way it does, you can fix it when it breaks, and you can answer it in an exam or an interview without help. That is the skill that gets you a job — not a copied answer.

Get certified — just ₹10

Finish all 35 lessons, pass the test (80%), pay ₹10 and get your certificate.

Take the test

Prefer watching? Video courses teach the same topics step by step.

Go further with a full course

Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.

Summer Industrial Training in MERN Stack 2026
₹3,000
View
Summer Training in Python Programming 2026
₹3,000
View
Ethical Hacking & Cyber Security Workshop
View
Browse all courses
JavaScript — Lessons
1. JavaScript Tutorial for Beginners — Learn JS from Scratch 2. Where to Put JavaScript Code — Head, Body or .js File 3. JavaScript Output Methods — alert, innerHTML, console.log 4. JavaScript Syntax Explained — Statements & Rules 5. JavaScript Comments — How to Write // and /* */ 6. JavaScript Variables — var, let, const Explained 7. JavaScript Operators — Arithmetic, Comparison, Logical 8. JavaScript Data Types — string, number, object, array 9. JavaScript Functions Tutorial — Parameters & Return Values 10. JavaScript Objects — Properties, Methods & this Keyword 11. JavaScript Scope — Global, Local & Block Scope Explained 12. JavaScript Events — addEventListener with Examples 13. JavaScript Strings — Methods, slice, replace & Templates 14. JavaScript Numbers — parseInt, toFixed, NaN Explained 15. JavaScript Arrays — Create, Read, push, pop & More 16. JavaScript Array Methods — map, filter, forEach, find 17. JavaScript Date Object — new Date(), getFullYear & More 18. JavaScript Math Object — random, round, floor, ceil 19. JavaScript if else — Conditions with Examples 20. JavaScript switch Statement — case, break, default 21. JavaScript for Loop — for, for...of, for...in Examples 22. JavaScript while Loop — while & do...while Explained 23. JavaScript Type Conversion — Number, String, Boolean 24. JavaScript try catch — Error Handling with finally 25. JavaScript JSON — stringify & parse with Examples 26. JavaScript Arrow Functions — => Syntax & this 27. JavaScript Classes — Constructor & Objects Explained 28. JavaScript Promises & async/await Explained Simply 29. JavaScript DOM Tutorial — Find & Change Page Elements 30. JavaScript Form Validation Tutorial with Examples 31. JavaScript Fetch API Tutorial — fetch() with Examples 32. JavaScript localStorage — setItem, getItem Explained 33. JavaScript Timers — setTimeout & setInterval Examples 34. JavaScript Regular Expressions — test() & match() Guide 35. JavaScript Debugging — console.log & Fixing Errors
Final Test — Certificate ₹10

🤖 KAMALDNP AI Assistant

Ask about courses, website & more

Hi! I'm KAMALDNP AI Assistant. Ask me about our courses, website features, or anything else! 😊
Please wait 5s...
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Please log in to download. You will stay on this page after logging in!

New here? Register now →