JS Variables
In this page:
var variableName = value;
let variableName = value;
const variableName = value;
var, let और const एक नज़र में
var variable घोषित करने का मूल तरीका है, जो function-scoped और अपने नियमों में कुछ ढीला है, जबकि let और const, जो बाद में आए, block-scoped हैं और आधुनिक code के लिए ज़्यादा पूर्वानुमेय व्यवहार देते हैं।
उदाहरण: var, let, and const at a Glance
// 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 देने देती है।
उदाहरण: Declaration and Initialization
let city; // declaration
city = "Paris"; // initialization
console.log(city);
नामकरण के नियम
Variable नाम किसी अक्षर, underscore या dollar sign से शुरू होने चाहिए, पहले character के बाद उनमें अंक हो सकते हैं, आरक्षित keywords इस्तेमाल नहीं हो सकते, और वे case sensitive होते हैं।
उदाहरण: Naming Rules
// 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 चलने तक पहुँच से बाहर रहते हैं।
उदाहरण: Hoisting
console.log(hoistedVar); // undefined, not an error
var hoistedVar = 5;
var, let और const में से चुनना
आधुनिक JavaScript में, जिस value को दोबारा assign नहीं किया जाएगा उसके लिए const डिफ़ॉल्ट पसंद है, let तब इस्तेमाल होता है जब value को सचमुच समय के साथ बदलना हो, और var को नए code में आमतौर पर उसके ढीले, कम पूर्वानुमेय scoping नियमों के कारण टाला जाता है।
उदाहरण: Choosing Between var, let, and const
const pi = 3.14159; // never reassigned
let counter = 0; // will change
counter = counter + 1;
console.log(pi, counter);
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: