Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🟨 Days 1–7 – JavaScript की नींव  ·  दिन 02

दिन 2: Variables, Data Types और Template Literals

⏱ 80 min📶 Beginner🎯 100% self-study
दिन 2 / 30

✅ आज आप सीखेंगे

  • let और const से values रखना (और var से क्यों बचते हैं)
  • Naming नियम और अच्छे नाम
  • Data types: string, number, boolean, null, undefined, bigint, symbol, object
  • typeof और types के बीच बदलना
  • साफ़ messages के लिए template literals

🏢 असली दुनिया में

हर program चीज़ें याद रखता है: product का नाम, उसका price, customer कितने kg चाहता है, delivery मुफ़्त है या नहीं। Variables उन values के लिए नाम वाले डिब्बे हैं। एक दुकान के billing page का असली bug: “2 kg × ₹900 दिखाता है ₹9009002” – quantity text के रूप में रखी थी। आज आप सीखेंगे ऐसा ठीक-ठीक क्यों होता है और कैसे रोकें।

let और const

JavaScript
const shopName = 'Sharma Sweets';   // will never change → const
let cartItems = 0;                   // will change → let

cartItems = cartItems + 1;
cartItems = cartItems + 2;
console.log(shopName, 'cart:', cartItems);

// shopName = 'Other';   ← TypeError: Assignment to constant variable.
Sharma Sweets cart: 3
Keywordदोबारा assign?Scopeकब
const ⭐नहींBlock { }Default – ज़्यादातर values नहीं बदलतीं
letहाँBlock { }Counters, totals, बदलने वाली values
varहाँFunction (blocks से बाहर निकलता है)सिर्फ़ पुराना code – नए में बचें
💡 आसान नियम

हर variable const से शुरू करें। let तभी करें जब सच में दोबारा assign करना हो। Code पढ़ना आसान होता है क्योंकि पढ़ने वाले जानते हैं कौन सी values बदल सकती हैं।

Variables के नाम

बुरे नाम

  • x, a1, data2
  • TOTALAMOUNTOFBILL
  • total-price (invalid: hyphen)
  • 2ndItem (invalid: अंक से शुरू)

अच्छे नाम (camelCase)

  • pricePerKg, quantityKg
  • isDeliveryFree (booleans is/has से)
  • customerName, orderTotal
  • Fixed settings के लिए MAX_ITEMS

Data types

JavaScript
const name = 'Kaju Katli';     // string
const price = 900;              // number (integers and decimals)
const rating = 4.8;             // number
const inStock = true;           // boolean
let discountCode;               // undefined – declared, no value yet
const gstNumber = null;         // null – intentionally empty
const bigOrderId = 9007199254740993n;  // bigint
const product = { name, price }; // object
const sizes = ['250g', '500g', '1kg']; // array (also an object)

console.log(typeof name, typeof price, typeof inStock);
console.log(typeof discountCode, typeof gstNumber, typeof bigOrderId);
console.log(typeof product, typeof sizes, Array.isArray(sizes));
string number boolean undefined object bigint object object true
🧠 दो मशहूर अजीब बातें

typeof null "object" है – 1995 का bug जो compatibility के लिए रखा गया। और arrays भी objects हैं, इसलिए arrays जाँचने को Array.isArray() लें। Interviewers दोनों पसंद करते हैं।

Types बदलना – 9009002 bug

JavaScript
const qtyFromInput = '2';          // everything from an <input> is a STRING
const price = 900;

console.log('Wrong:', qtyFromInput + price);           // joins text!
console.log('Wrong:', qtyFromInput * price);           // works by luck (* converts)
console.log('Right:', Number(qtyFromInput) * price);   // convert first ✅

console.log(Number('2.5'), Number(''), Number('abc'));
console.log(parseInt('250g'), parseFloat('1.5kg'));
console.log(String(1800), (1800).toString(), Boolean(0), Boolean('hi'));
Wrong: 2900 Wrong: 1800 Right: 1800 2.5 0 NaN 250 1.5 1800 1800 false true
Functionउदाहरणनतीजा
Number()Number('2.5')2.5 (number न हो तो NaN)
parseInt / parseFloatparseInt('250g')250 – पहले non-digit तक अंक पढ़ता है
String()String(1800)'1800'
Boolean()Boolean('')false – ख़ाली string, 0, null, undefined, NaN falsy हैं
Number.isNaN()Number.isNaN(Number("abc"))true – number इस्तेमाल से पहले जाँचें

Template literals

JavaScript
const customer = 'Aarti';
const item = 'Kaju Katli';
const qty = 2;
const price = 900;

// Old way – hard to read
console.log('Hi ' + customer + ', ' + qty + ' kg ' + item + ' = Rs ' + qty * price);

// Template literal – backticks and ${ }
console.log(`Hi ${customer}, ${qty} kg ${item} = ₹${qty * price}`);

// Multi-line text is easy too
const sms = `Sharma Sweets 🍬
Order confirmed for ${customer}.
Total: ₹${qty * price}`;
console.log(sms);
Hi Aarti, 2 kg Kaju Katli = Rs 1800 Hi Aarti, 2 kg Kaju Katli = ₹1800 Sharma Sweets 🍬 Order confirmed for Aarti. Total: ₹1800

असली task: bill calculator

HTML + JavaScript
<label>Quantity (kg) <input id="qty" type="number" value="2" min="0.25" step="0.25"></label>
<button id="calc">Calculate</button>
<pre id="bill"></pre>

<script>
const PRICE_PER_KG = 900;
const GST_RATE = 0.05;   // 5% GST on sweets

document.querySelector('#calc').addEventListener('click', function () {
  const qty = Number(document.querySelector('#qty').value);   // string → number
  const amount = qty * PRICE_PER_KG;
  const gst = amount * GST_RATE;
  const total = amount + gst;
  document.querySelector('#bill').textContent =
    `Kaju Katli  ${qty} kg × ₹${PRICE_PER_KG}
Amount      ₹${amount.toFixed(2)}
GST 5%      ₹${gst.toFixed(2)}
Total       ₹${total.toFixed(2)}`;
});
</script>
▶  Change the quantity and calculate

🏅 आज के Job Skills

✔ const या let सही चुनना✔ camelCase में साफ़ नाम✔ typeof से data types पहचानना✔ Input strings को numbers में बदलना और template literals से messages

Practice tasks

🏋️ Practice – ख़ुद करें

Task 1: एक student का नाम, 3 subjects के marks (100 में से) रखें और template literal से total व percentage print करें।
Solution देखें
JavaScript
const student = 'Riya';
const maths = 88, science = 92, english = 79;
const total = maths + science + english;
const percent = (total / 300) * 100;
console.log(`${student}: ${total}/300 = ${percent.toFixed(1)}%`);
Riya: 259/300 = 86.3%
Task 2: पहले अंदाज़ा लगाएँ, फिर चलाएँ: "5" + 3, "5" - 3, "5" * "2", true + 1।
Solution देखें
JavaScript
console.log("5" + 3);   // "53"  (+ with a string joins)
console.log("5" - 3);   // 2     (- converts to number)
console.log("5" * "2"); // 10
console.log(true + 1);  // 2     (true becomes 1)
53 2 10 2
Task 3: ऐसा “EMI preview” बनाएँ जो inputs से loan राशि और महीने पढ़कर राशि ÷ महीने दिखाए (अभी ब्याज नहीं)।

Quick quiz

🧠 Quick Quiz – ख़ुद को जाँचें

1. Default keyword कौन सा हो?
दोबारा assign हो तभी let।
2. '10' + 5 का मान?
String जुड़ना।
3. typeof null है…
पुराना bug।
4. <input> से values होती हैं…
Number() से बदलें।
5. Template literals में…
Backtick key 1 के बाईं ओर।

Interview corner

💼 Interview Corner

let, const और var में फ़र्क?

var function-scoped है, undefined के साथ hoist होता है और दोबारा declare हो सकता है; let और const block-scoped हैं और declare होने तक temporal dead zone में। const दोबारा assign नहीं होता (पर जिस object की ओर इशारा करे वह बदल सकता है)।

null बनाम undefined?

undefined का मतलब variable है पर अभी value नहीं (JavaScript देता है); null programmer का जानबूझकर दिया “कोई value नहीं” है। typeof undefined "undefined", typeof null "object"; null == undefined true पर null === undefined false।

सारांश

  • Default में const, दोबारा assign पर let, var से बचें।
  • camelCase, अर्थपूर्ण नाम; booleans is/has से।
  • typeof type बताता है; inputs strings हैं – Number() से बदलें।
  • Template literals: `Total ₹${total}`।
➡️ आगे

Day 3: operators और conditions – if, else और switch से discounts, free delivery नियम और grade calculators।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

क्या const array बदल सकता है?
हाँ – variable दोबारा assign नहीं कर सकते, पर array में items push या object properties बदल सकते हैं। Day 7 copies बनाना सिखाता है।
0.1 + 0.2 = 0.30000000000000004 क्यों?
Numbers binary floating point में रखे जाते हैं। पैसों के लिए paise (integers) में काम करें या दिखाने को toFixed(2) से round करें – Day 9 में।
← JavaScript (30 Days)
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicदिन 3: Operators, Conditions और फ़ैसले आपकी प्रगति: 2 / 30 (7%) · Lesson 3 / 30 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें