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

JS Variables

Think of a labeled storage box in a warehouse, you write a name on the box, put something inside it, and from then on anyone who knows the label can find and use whatever's stored there. A variable in JavaScript works the same way, it's a named container that holds a value, a number, a piece of text, or something more complex, so your code can refer back to that value by name instead of retyping it everywhere. JavaScript gives you three ways to create these containers, var, let, and const, and knowing when to use each is one of the first real decisions you make writing any script, from a tiny demo to a full application like the ones behind cookiescursor.com.
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. #}
🔒

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.