Feedback Form

Variable in javascript in hindi

JavaScript Variables Explained in Hindi

JavaScript Variables in Hindi

What are JavaScript Variables?

JavaScript में Variables ऐसे कंटेनर्स होते हैं, जिनमें हम डेटा को स्टोर कर सकते हैं। जैसे कि किसी डिब्बे में हम चीजें रखते हैं, वैसे ही हम Variables में डेटा रखते हैं। Variables का इस्तेमाल प्रोग्रामिंग में डेटा को manipulate करने के लिए किया जाता है।

Declaring Variables in JavaScript in Hindi

Variables को JavaScript में declare करने के लिए var, let, या const का इस्तेमाल किया जाता है। यह तीनों की अपनी-अपनी विशेषताएँ हैं।

  • var: पुराना तरीका है, लेकिन अब इसे कम इस्तेमाल किया जाता है क्योंकि इसकी scope की वजह से कई समस्याएँ होती हैं।
  • let: यह block-level scope के साथ आता है, जिससे यह ज्यादा सुरक्षित और reliable होता है।
  • const: यह value को constant (अपरिवर्तनीय) बना देता है, यानी एक बार value assign करने के बाद उसे बदला नहीं जा सकता।

Types of Variables in JavaScript in Hindi

JavaScript में मुख्य रूप से तीन प्रकार के variables होते हैं:

  • Local Variables: यह केवल उस function या block के अंदर accessible होते हैं जहाँ इन्हें declare किया गया है।
  • Global Variables: यह पूरी प्रोग्राम में कहीं भी accessible होते हैं, अगर इन्हें global scope में declare किया गया हो।
  • Block-Level Variables: इन्हें let या const के साथ declare किया जाता है और ये केवल block के अंदर ही available होते हैं।

Variable Scope in JavaScript in Hindi

Variable scope से मतलब है कि किसी variable को हम कहाँ और किस context में access कर सकते हैं। JavaScript में variable का scope तीन तरह का होता है:

  • Global Scope: Global variables को पूरी JavaScript code में कहीं भी access किया जा सकता है।
  • Local Scope: Local variables केवल उस function या block में available होते हैं जहाँ इन्हें declare किया गया है।
  • Block Scope: let और const variables block-level scope में होते हैं, यानी यह केवल block के अंदर ही valid होते हैं।

Assigning Values to Variables in JavaScript in Hindi

JavaScript में variable को value assign करने के लिए हम assignment operator = का इस्तेमाल करते हैं। उदाहरण के लिए:

let age = 25;

यहाँ, age variable को value 25 assign की गई है।

Variables में किसी भी प्रकार का डेटा store किया जा सकता है, जैसे numbers, strings, arrays, objects, etc.

Examples and Exercises on JavaScript Variables in Hindi

अब हम कुछ practical examples देखेंगे, जो आपको JavaScript में variables को समझने में मदद करेंगे।

Example 1: Declaring and Assigning Values to Variables

let name = "Rahul";
let age = 20;
console.log(name); // Output: Rahul
console.log(age); // Output: 20

इस example में, name और age variables को values assign की गई हैं, और फिर उन्हें console.log() के जरिए output किया गया है।

Example 2: Variable Scope Example

let x = 10;
if (x > 5) {
let y = 20;
console.log(x); // Output: 10
console.log(y); // Output: 20
}
console.log(y); // Error: y is not defined

यह example variable scope को demonstrate करता है। यहाँ y variable केवल if block में valid है, बाहर इसे access किया तो error आएगा।

Exercise: Create a Simple Program

अभी आप एक simple program बनाएँ, जो user से उनका नाम और आयु ले, फिर उसे print करें।

let name = prompt("Enter your name:");
let age = prompt("Enter your age:");
console.log("Name: " + name);
console.log("Age: " + age);

यह exercise आपको variable assignment और user input handling को समझने में मदद करेगी।

FAQs

In JavaScript, a variable is used to store data values. It acts as a container that holds data that can be accessed and modified during the execution of the program. (JavaScript में, एक Variable डेटा मानों को स्टोर करने के लिए उपयोग किया जाता है। यह एक कंटेनर की तरह काम करता है जो डेटा को रखता है, जिसे प्रोग्राम के निष्पादन के दौरान एक्सेस और संशोधित किया जा सकता है।)

To declare a variable in JavaScript, you can use var, let, or const. Each of them has different characteristics. (JavaScript में, एक Variable को घोषित करने के लिए आप var, let, या const का उपयोग कर सकते हैं। इन तीनों की अलग-अलग विशेषताएँ हैं।)

var has function scope, meaning it is accessible throughout the function. let has block-level scope, so it’s only available within the block where it’s declared. const is used to declare constants that cannot be reassigned. ( var का function scope होता है, यानी यह पूरी function में उपलब्ध होता है। let का block-level scope होता है, इसलिए यह केवल उस block के भीतर उपलब्ध होता है जहाँ इसे घोषित किया गया है। const का उपयोग constants को घोषित करने के लिए किया जाता है जिन्हें पुनः असाइन नहीं किया जा सकता। )

Variable scope defines where a variable can be accessed in the program. It can either be global, local, or block-scoped. The scope helps in determining the lifespan and accessibility of the variable. (Variable का Scope यह निर्धारित करता है कि एक variable को प्रोग्राम में कहां एक्सेस किया जा सकता है। यह या तो global, local, या block-scoped हो सकता है। Scope यह निर्धारित करने में मदद करता है कि variable की जीवनकाल और उपलब्धता कहाँ तक है।)

You assign a value to a variable in JavaScript using the assignment operator (=). For example: let age = 30; assigns the value 30 to the variable age. (JavaScript में एक variable को value assign करने के लिए हम assignment operator (=) का उपयोग करते हैं। उदाहरण के लिए: let age = 30; variable age को value 30 assign करता है।)

Common errors include using variables before declaring them, declaring a variable more than once in the same scope, and not following the correct variable naming conventions. (सामान्य गलतियाँ में शामिल हैं, variables को declare करने से पहले उनका उपयोग करना, एक ही scope में एक variable को एक से ज्यादा बार declare करना, और सही variable naming conventions का पालन न करना।)