📘 Lesson · Lesson 06
Variables: var, let, const
Variables: var, let, const
What Variables Are
let score = 10;
let playerName = "Aman";
let isGameOver = false;
A variable is a named container that stores a value. Think of it as a labelled box: you give it a name (like
score) and put a value inside (like 10), and later you can use that name to get or change the value. Variables are the foundation of all programming — they let your code remember and work with data: a user's name, a running total, a game score, whether a button is clicked. Almost every line of useful JavaScript involves variables. The label lets you refer to the stored value anywhere in your code.Declaring with let
let age = 20; // declare and assign a value
console.log(age); // 20
age = 21; // change (reassign) the value later
console.log(age); // 21
let city; // declare without a value (it's "undefined")
city = "Aligarh"; // assign later
let declares a variable whose value CAN be changed later. You write let, the name, then = and the value. The single = means "assign" (put this value into this variable) — not "equals" in the maths sense. Later, you can change the value by writing just the name and = (no let the second time — that would re-declare it). This ability to reassign is what makes let useful for values that change over time: a score that goes up, a counter, a user input. Use let whenever a variable's value will need to change.const — for values that don't change
const pi = 3.14159;
const siteName = "CodeKaFunda";
const maxUsers = 100;
pi = 3.14; // ERROR! You cannot reassign a const
const declares a "constant" — a variable whose value must NOT change after it's set. If you try to reassign a const, JavaScript throws an error. This might sound limiting, but it's actually a safety feature: use const for values that should stay fixed (a tax rate, a site name, a configuration setting), and JavaScript will PROTECT them from accidental changes. It also makes your code clearer — a reader sees const and immediately knows "this value never changes." Importantly, a const must be given a value when declared (you can't declare an empty const and fill it later).var — the old way (avoid it)
var oldStyle = "This is how variables were declared before 2015";
// var still works, but let and const are preferred in modern JavaScript
var is the original way to declare variables, from before 2015 (ES6). It still works, so you'll see it in older code and tutorials, but it has some confusing behaviours (around scope and hoisting, which you'll meet later) that led to bugs. Modern JavaScript replaced it with let and const, which are safer and more predictable. The simple rule: don't use var in new code. Recognise it when you see it in older examples, but reach for let or const in everything you write. This is universal advice in the modern JavaScript community.Which One to Use — the simple rule
| Keyword | Can reassign? | When to use |
|---|---|---|
| const | No | Default choice — use for everything that won't change |
| let | Yes | When the value WILL change (counters, scores, inputs) |
| var | Yes | Avoid — old style |
The professional convention is simple: default to
const, and use let only when you know the value needs to change. Start by declaring everything const; if you later find you need to reassign a variable, switch that one to let. Why prefer const? Because it prevents accidental changes and signals your intent clearly. This "const by default, let when needed" habit is followed by most modern JavaScript developers and leads to safer, more readable code. And never use var in new code. Simple, and it'll serve you well throughout this whole course.Exam Corner
Q: What is a variable? A named container that stores a value your code can use and change.
Q: Difference between let and const?
Q: What does a single = mean in JavaScript? Assignment — putting a value into a variable (not mathematical equality).
Q: Should you use var in new code? No — use let and const; var is the outdated style.
Q: What's the recommended default: let or const? const — use it by default, and switch to let only when a value must change.
Q: Difference between let and const?
let can be reassigned; const cannot be changed after it's set.Q: What does a single = mean in JavaScript? Assignment — putting a value into a variable (not mathematical equality).
Q: Should you use var in new code? No — use let and const; var is the outdated style.
Q: What's the recommended default: let or const? const — use it by default, and switch to let only when a value must change.
Variables क्या हैं
let score = 10;
let playerName = "Aman";
let isGameOver = false;
Variable एक named container है जो value store करता है. इसे labelled box समझिए: आप इसे नाम देते हैं (जैसे
score) और अंदर value रखते हैं (जैसे 10), और बाद में उस नाम से value पा या बदल सकते हैं. Variables सभी programming की नींव हैं — वे आपके code को data याद रखने और उसके साथ काम करने देते हैं: user का नाम, चलता total, game score, button click हुआ या नहीं. उपयोगी JavaScript की लगभग हर line variables शामिल करती है. Label आपको stored value को अपने code में कहीं भी refer करने देता है.let से Declare करना
let age = 20; // declare aur value assign
console.log(age); // 20
age = 21; // baad me value badlo (reassign)
console.log(age); // 21
let city; // bina value ke declare (yeh "undefined")
city = "Aligarh"; // baad me assign
let ऐसा variable declare करता है जिसका value बाद में BADLA जा सकता है. आप let, नाम, फिर = और value लिखते हैं. एकल = मतलब "assign" (यह value इस variable में रखो) — maths वाले "equals" नहीं. बाद में, आप सिर्फ नाम और = लिखकर value बदल सकते हैं (दूसरी बार let नहीं — वह उसे re-declare करेगा). Reassign करने की यह क्षमता let को समय के साथ बदलने वाले values के लिए उपयोगी बनाती है: बढ़ता score, counter, user input. जब भी variable का value बदलने की ज़रूरत हो let use कीजिए.const — न बदलने वाले values के लिए
const pi = 3.14159;
const siteName = "CodeKaFunda";
const maxUsers = 100;
pi = 3.14; // ERROR! const reassign nahi kar sakte
const एक "constant" declare करता है — ऐसा variable जिसका value set होने के बाद बदलना NAHI चाहिए. अगर आप const reassign करने की कोशिश करें, JavaScript error देता है. यह सीमित लग सकता है, पर असल में safety feature है: fixed रहने वाले values के लिए const use कीजिए (tax rate, site नाम, configuration setting), और JavaScript उन्हें आकस्मिक बदलाव से BACHAYEGA. यह आपका code भी साफ बनाता है — reader const देखकर तुरंत जानता है "यह value कभी नहीं बदलती." अहम बात, const को declare करते समय value देना ज़रूरी है (खाली const declare करके बाद में नहीं भर सकते).var — पुराना तरीका (टालिए)
var oldStyle = "Yeh 2015 se pehle variables declare karne ka tarika tha";
// var abhi bhi chalta hai, par modern JavaScript me let aur const pasand hain
var variables declare करने का मूल तरीका है, 2015 (ES6) से पहले से. यह अभी भी चलता है, तो आप इसे पुराने code और tutorials में देखेंगे, पर इसके कुछ उलझाने वाले व्यवहार हैं (scope और hoisting के आसपास, जो आप बाद में मिलेंगे) जो bugs की ओर ले गए. Modern JavaScript ने इसे let और const से replace किया, जो सुरक्षित और ज़्यादा predictable हैं. सरल rule: नए code में var use मत कीजिए. पुराने examples में इसे देखकर पहचानिए, पर जो कुछ आप लिखें उसमें let या const लीजिए. यह modern JavaScript community में universal सलाह है.कौन-सा Use करें — सरल rule
| Keyword | Reassign? | कब use करें |
|---|---|---|
| const | नहीं | Default choice — न बदलने वाली हर चीज़ के लिए |
| let | हां | जब value बदलेगा (counters, scores, inputs) |
| var | हां | टालिए — पुरानी style |
Professional convention सरल है: default में
const, और let सिर्फ तब जब आप जानें value बदलने की ज़रूरत है. हर चीज़ const declare करके शुरू कीजिए; अगर बाद में पता चले किसी variable को reassign करना है, उस एक को let में switch कीजिए. const क्यों पसंद? क्योंकि यह आकस्मिक बदलाव रोकता है और आपका इरादा साफ दिखाता है. यह "const by default, ज़रूरत पर let" आदत ज़्यादातर modern JavaScript developers follow करते हैं और सुरक्षित, ज़्यादा readable code की ओर ले जाती है. और नए code में var कभी use मत कीजिए. सरल, और यह पूरे course में आपका साथ देगा.Exam Corner
Q: Variable क्या है? Named container जो value store करता है जिसे आपका code use और बदल सकता है.
Q: let और const में अंतर?
Q: JavaScript में एकल = का क्या मतलब? Assignment — variable में value रखना (गणितीय equality नहीं).
Q: क्या नए code में var use करना चाहिए? नहीं — let और const use कीजिए; var पुरानी style है.
Q: Recommended default: let या const? const — इसे default में use कीजिए, और value बदलने पर ही let में switch कीजिए.
Q: let और const में अंतर?
let reassign हो सकता है; const set होने के बाद बदला नहीं जा सकता.Q: JavaScript में एकल = का क्या मतलब? Assignment — variable में value रखना (गणितीय equality नहीं).
Q: क्या नए code में var use करना चाहिए? नहीं — let और const use कीजिए; var पुरानी style है.
Q: Recommended default: let या const? const — इसे default में use कीजिए, और value बदलने पर ही let में switch कीजिए.
💻 Live Code Editor
इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.
सब कुछ आपके browser में ही चलता है — कोई server, कोई signup नहीं. JavaScript का
console.log देखने के लिए F12 दबाइए.