जावास्क्रिप्ट में कथन

Jan 24 2023
जावास्क्रिप्ट स्टेटमेंट कोड का एक ब्लॉक है जो कुछ क्रिया या कार्य को पूरा करता है। डिफ़ॉल्ट रूप से, जावास्क्रिप्ट दुभाषिया इन बयानों को लिखे जाने के क्रम में एक के बाद एक निष्पादित करता है।

जावास्क्रिप्ट स्टेटमेंट कोड का एक ब्लॉक है जो कुछ क्रिया या कार्य को पूरा करता है। डिफ़ॉल्ट रूप से, जावास्क्रिप्ट दुभाषिया इन बयानों को लिखे जाने के क्रम में एक के बाद एक निष्पादित करता है।

आइए नीचे जेएस में विभिन्न प्रकार के बयानों को समझने की कोशिश करते हैं

1. अभिव्यक्ति कथन

ऐसे एक्सप्रेशन जिनके साइड इफेक्ट होते हैं, उन्हें एक्सप्रेशन स्टेटमेंट जैसे असाइनमेंट स्टेटमेंट, फंक्शन इनवोकेशन, इंक्रीमेंट और डिक्रीमेंट ऑपरेटर्स, ++ और - असाइनमेंट स्टेटमेंट से संबंधित और डिलीट ऑपरेटर्स के रूप में माना जाता है।

let greeting = “Hello “ + name; //assignment statement
counter ++; // increment operator which is changing a variable value
delete o.x; // deleting a property of an object O
cx = Math.cos(x);; // function invocation and assign the value to a variable

एक एकल कथन में एक से अधिक उप कथन हो सकते हैं जिन्हें JS में मिश्रित कथन या कथन ब्लॉक माना जाता है। निम्नलिखित पंक्तियाँ एकल कथन के रूप में कार्य करती हैं

{ 
 x = Math.PI; 
 cx = Math.cos(x);
 console.log(“cos(π) = “ + cx);
}

3. सशर्त बयान

कथन जो JS दुभाषिया को क्रियान्वित करते हैं या अभिव्यक्ति के मूल्य के आधार पर अन्य कथनों को छोड़ते हैं जैसे if और switch बयान।

3.1 if Statement
मौलिक नियंत्रण कथन है जो JS को निर्णय लेने और बयानों को सशर्त रूप से निष्पादित करने की अनुमति देता है।

//syntax
If (expression) 
 Statement1
else 
 Statement 2

//example
if (n === 1) { //curly braces are optional in case of single statement
 console.log("You have 1 new message.");
} else {
 console.log(`You have ${n} new messages.`);
}

3.2 स्विच स्टेटमेंट
जब एक ही अभिव्यक्ति के मूल्य का उपयोग 2 से अधिक विकल्पों के बीच निर्णय लेने के लिए किया जा सकता है, ऐसे मामलों के लिए जेएस एक स्विच स्टेटमेंट प्रदान करता है।

//Syntax
switch(expression) {
 case label1: statement1; // if expression value === label1 execute statement1
 case label2: statement2; // if expression value === label2 execute statement2
 … 
 default: default_statement; // optional
}

//Example:
switch(n) {
  case 1:                        
   // Start here if n === 1.(sameness is determined by the === operator)
   // Execute code block #1.
   break;                         // stop here
  case 2:                         
    // Start here if n === 2
    // Execute code block #2.
   break;                         // Stop here
  case 3:                         
    // Start here if n === 3
    // Execute code block #3;
   break;                         // Stop here
  default:                        
    // If no case match the value of n
    // Execute code block #4.
}

4. लूप्स स्टेटमेंट

कथन जो अन्य कथनों को बार-बार निष्पादित करते हैं, जैसे कि थोड़ी देर के लिए और के लिए।

4.1 जबकि पाश
दुभाषिया पहले अभिव्यक्ति का मूल्यांकन करता है । यदि अभिव्यक्ति का मूल्य गलत है, तो दुभाषिया लूप बॉडी के रूप में कार्य करने वाले कथन को छोड़ देता है और कार्यक्रम में अगले कथन पर चला जाता है। दूसरी ओर, यदि अभिव्यक्ति सत्य है, तो दुभाषिया कथन को निष्पादित करता है और इसे दोहराता है।

//Syntax
while (expression) { //curly braces are optional in case of single statement
  //statment or statement block
}

//Example
let count = 0;
while(count < 10) {
 console.log(count);
 count++;
}

//Syntax
do { //curly braces are optional in case of a single statement
 //statement / statement block
} 
while (expression);

//Example
let count = 0;
do {
 console.log(count);
 count++;
}
while (count < 10);

//syntax
for(initialize ; test ; increment) { //curly braces are optional in case of single statement
 //statement / statement block
}
//Example:
for(let count = 0; count < 10; count++) {
 console.log(count);
}

//Syntax with example
let data = [1, 2, 3, 4, 5, 6, 7, 8, 9], sum = 0;
for(let element of data) {
 sum += element;
}

// for of with map
let m = new Map([[1, "one"]]);
for(let [key, value] of m) {
 key    // => 1
 value  // => "one"
}

//syntax
for (variable in object) {
 //statement / statement block
}

//Example:
for(let p in o) { // Assign property names of o to variable p
 console.log(o[p]); // Print the value of each property
}

बयान जो दुभाषिया को कार्यक्रम के दूसरे हिस्से में कूदने का कारण बनते हैं, जैसे ब्रेक , रिटर्न और थ्रो ।

5.1 लेबल किया गया
किसी भी कथन को पहचानकर्ता और कोलन के साथ पहले लेबल किया जा सकता है: किसी कथन को लेबल करके, आप इसे एक नाम देते हैं जिसका उपयोग आप इसे अपने प्रोग्राम में कहीं और संदर्भित करने के लिए कर सकते हैं ।

//syntax
identifier: statement

//example
mainloop: while(token !== null) {
 // Code omitted…
 continue mainloop; // Jump to the next iteration of the named loop(mainloop)
 // More code omitted…
}

5.2 जारी रखें
जब जारी बयान निष्पादित किया जाता है, तो संलग्न लूप का वर्तमान पुनरावृत्ति समाप्त हो जाता है, और अगला पुनरावृत्ति शुरू होता है।

//example
for(let i = 0; i < data.length; i++) {
 if (!data[i]) continue; // jump to the next iteration of the loop
 total += data[i];
}

//syntax
return expression;

//Example
function square(x) { return x*x; } // A function that has a return statement
square(2) // => 4

// A generator function that yields a range of integers
function* range(from, to) {
  for(let i = from; i <= to; i++) {
    yield i;
  }
}

कोशिश/पकड़/आखिरकार
कोशिश/पकड़/आखिरकार बयान जावास्क्रिप्ट का अपवाद-हैंडलिंग तंत्र है।

//syntax
try {
 //Normally, this code runs from the top of the block to the bottom
 //without problems. But it can sometimes throw an exception,
 //either directly or with a throw statement.
} catch(e) {
 // The statements in this block are executed if, and only if, the try
 // block throws an exception. These statements can use the local variable
 // e to refer to the Error object or other value that was thrown.
} finally {
 // This block contains statements that are always executed, regardless of
 // what happens in the try block. They are executed whether the try
 // block terminates:
}

//example
try {
  // Ask the user to enter a number
  let n = Number(prompt("Please enter a positive integer", ""));
  // Compute the factorial of the number, assuming the input is valid
  let f = factorial(n);
  // Display the result
  alert(n + "! = " + f);
} catch(e) {     // If the user's input was not valid, we end up here
  alert(e);  // Tell the user what the error is
}

ये अगली कुछ पोस्ट जावास्क्रिप्ट पर मूल बातें और उन्नत विषयों पर चर्चा करने वाली श्रृंखला का हिस्सा होंगी।

अगर आपको यह कहानी अच्छी लगी हो, तो कृपया बटन पर क्लिक करें और इसे साझा करें, ताकि अन्य लोग भी इसे ढूंढ सकें! साथ ही, नीचे एक टिप्पणी छोड़ने के लिए स्वतंत्र महसूस करें।

Groww Engineering तकनीकी उपाख्यानों, नवीनतम तकनीकों और सामान्य प्रोग्रामिंग समस्याओं से निपटने के बेहतर तरीकों को प्रकाशित करता है। नवीनतम अपडेट प्राप्त करने के लिए आप यहां सदस्यता ले सकते हैं।

हम भर्ती कर रहे हैं। नौकरी के उद्घाटन यहां देखें ।