SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
Lesson 6 of 30

JavaScript Variables

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

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

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 30 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.

CapCut Mobile Video Editing Course Beginner to Advance
₹99
View
AI & ML with Azure AI, AWS AI & Generative AI
₹10,000
View
Master Python + AI Skills in 30 Days
₹499
View
Browse all courses
JavaScript — Lessons
1. JavaScript Introduction 2. JavaScript Where To 3. JavaScript Output 4. JavaScript Syntax 5. JavaScript Comments 6. JavaScript Variables 7. JavaScript Operators 8. JavaScript Data Types 9. JavaScript Functions 10. JavaScript Objects 11. JavaScript Scope 12. JavaScript Events 13. JavaScript Strings 14. JavaScript Numbers 15. JavaScript Arrays 16. JavaScript Array Methods 17. JavaScript Dates 18. JavaScript Math 19. JavaScript Conditions 20. JavaScript Switch 21. JavaScript For Loops 22. JavaScript While Loops 23. JavaScript Type Conversion 24. JavaScript Errors — try, catch, finally 25. JavaScript JSON 26. JavaScript Arrow Functions 27. JavaScript Classes 28. JavaScript Promises and async/await 29. JavaScript and the DOM 30. JavaScript Form Validation
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...
🛡️ 7-Day Money-Back Guarantee
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Download karne ke liye login karo. Login ke baad yahi page pe rahoge!

Naya ho? Register karo →