← Back to JavaScript Course | Chapter 1: Basics & Syntax | Lesson 7 of 12

JS वेरिएबल्स

किसी warehouse में एक labeled storage box के बारे में सोचें, आप box पर एक नाम लिखते हैं, उसके अंदर कुछ रखते हैं, और उसके बाद वह label जानने वाला कोई भी वहाँ रखी चीज़ ढूंढ और इस्तेमाल कर सकता है। JavaScript में एक variable भी बिल्कुल वैसे ही काम करता है, यह एक named container है जो कोई value रखता है, एक number, टेक्स्ट का एक टुकड़ा, या कुछ ज़्यादा जटिल, ताकि आपका code हर जगह उसे दोबारा टाइप करने के बजाय नाम से उस value को वापस refer कर सके। JavaScript आपको ये containers बनाने के तीन तरीके देता है, var, let, और const, और यह जानना कि कब किसे इस्तेमाल करना है, किसी भी script लिखते समय आपके पहले असली decisions में से एक है, चाहे वह एक छोटा demo हो या cookiescursor.com जैसा एक पूरा application।
Syntax
javascript
var variableName = value;
let variableName = value;
const variableName = value;

var, let और const एक नज़र में

var variable घोषित करने का मूल तरीका है, जो function-scoped और अपने नियमों में कुछ ढीला है, जबकि let और const, जो बाद में आए, block-scoped हैं और आधुनिक code के लिए ज़्यादा पूर्वानुमेय व्यवहार देते हैं।

Note: शुरुआती नियम के तौर पर, const को डिफ़ॉल्ट रखिए, let पर केवल तब जाइए जब आपको पता हो कि value बदलनी है, और नए code में var से बचिए।
Warning: var variables can be redeclared and are function-scoped, which can lead to bugs that let and const are specifically designed to prevent.

उदाहरण: var, let, and const at a Glance

javascript
// Declare the variable `oldStyle`, set to "function-scoped"
// Declare the variable `oldStyle`, set to "function-scoped"
var oldStyle = "function-scoped";
// Declare the variable `modern`, set to "block-scoped"
// Declare the variable `modern`, set to "block-scoped"
let modern = "block-scoped";
// Declare the constant `fixed`, set to "block-scoped, cannot reassign"
// Declare the constant `fixed`, set to "block-scoped, cannot reassign"
const fixed = "block-scoped, cannot reassign";
// Print `oldStyle, modern, fixed` to the console
// Print `oldStyle, modern, fixed` to the console
console.log(oldStyle, modern, fixed);

Declaration और Initialization

Declaration variable का नाम बनाने की क्रिया है, जबकि initialization उसे पहला value देना है, और JavaScript आपको दोनों एक साथ करने देती है या पहले variable घोषित करके बाद में उसे value देने देती है।

Note: const के लिए declaration और initialization का एक साथ होना ज़रूरी है, आप बिना तुरंत value दिए const घोषित नहीं कर सकते।
Warning: A let variable that is declared but never initialized holds the special value undefined until you assign something to it.

उदाहरण: Declaration and Initialization

javascript
let city; // declaration
city = "Paris"; // initialization
console.log(city);

नामकरण के नियम

Variable नाम किसी अक्षर, underscore या dollar sign से शुरू होने चाहिए, पहले character के बाद उनमें अंक हो सकते हैं, आरक्षित keywords इस्तेमाल नहीं हो सकते, और वे case sensitive होते हैं।

Note: वर्णनात्मक, विशिष्ट नाम चुनिए, जैसे n की जगह tutorialCount, codebase बड़ा होने पर इसका बहुत फ़ायदा मिलता है।
Warning: Starting a variable name with a number, like 1stPlace, is invalid and causes an immediate syntax error.

उदाहरण: Naming Rules

javascript
// Declare the variable `_underscoreStart`, set to `1`
// Declare the variable `_underscoreStart`, set to `1`
let _underscoreStart = 1;
let $dollarStart = 2;
// Declare the variable `tutorialCount`, set to `3`
// Declare the variable `tutorialCount`, set to `3`
let tutorialCount = 3;
// Print `_underscoreStart, $dollarStart, tutorialCount` to the console
// Print `_underscoreStart, $dollarStart, tutorialCount` to the console
console.log(_underscoreStart, $dollarStart, tutorialCount);

Hoisting

Hoisting JavaScript का वह व्यवहार है जिसमें code को line-by-line चलाने से पहले variable declarations को प्रोसेस किया जाता है, लेकिन var और let/const बहुत अलग व्यवहार करते हैं: var hoist होता है और undefined के रूप में initialize होता है, जबकि let और const hoist होते हैं पर अपनी declaration line चलने तक पहुँच से बाहर रहते हैं।

Note: भले ही var तकनीकी रूप से अपनी declaration line से पहले काम करता है, स्पष्टता के लिए हमेशा variables को उस scope के ऊपर घोषित कीजिए जिसमें आप उन्हें इस्तेमाल करते हैं।
Warning: Accessing a let or const variable before its declaration throws a ReferenceError, a safety feature var does not have.

उदाहरण: Hoisting

javascript
console.log(hoistedVar); // undefined, not an error
var hoistedVar = 5;

var, let और const में से चुनना

आधुनिक JavaScript में, जिस value को दोबारा assign नहीं किया जाएगा उसके लिए const डिफ़ॉल्ट पसंद है, let तब इस्तेमाल होता है जब value को सचमुच समय के साथ बदलना हो, और var को नए code में आमतौर पर उसके ढीले, कम पूर्वानुमेय scoping नियमों के कारण टाला जाता है।

Note: अगर let को const में बदलने के बाद भी आपका code ठीक चलता है, तो यह मज़बूत संकेत है कि const शुरू से बेहतर विकल्प था।
Warning: Choosing let everywhere out of uncertainty, even for values that never change, makes it harder for readers to spot which values actually do change.

उदाहरण: Choosing Between var, let, and const

javascript
const pi = 3.14159; // never reassigned
let counter = 0;     // will change
counter = counter + 1;
console.log(pi, counter);
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. नए code में आदतन var इस्तेमाल करना, जबकि modern JavaScript में let और const ज़्यादा safe, ज़्यादा predictable scoping देते हैं।
  2. किसी variable को declare होने से पहले इस्तेमाल करने की कोशिश करना, जो var, let, या const इस्तेमाल होने के हिसाब से अलग व्यवहार करता है।
  3. data या temp जैसे vague variable names चुनना, जो महीनों बाद code को समझना काफी मुश्किल बना देते हैं।
चैप्टर सारांश
  • एक variable किसी value के लिए एक named container है, जो var, let, या const से बनाया जाता है।
  • Declaration variable बनाता है, और initialization उसे उसकी पहली value देता है, ये एक साथ या अलग-अलग हो सकते हैं।
  • Variable names specific rules follow करने चाहिए और, hoisting की वजह से, var और let/const उनकी declaration line से पहले अलग व्यवहार करते हैं।
ब्राउज़र सपोर्ट

var, let, और const सभी हर modern browser में supported हैं, हालांकि let और const को एक reasonably modern engine चाहिए, 2015 से standard और आज universal।

🔒

Chapter Quiz — Complete all 12 topics to unlock

0/12 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.