diff --git a/1-js/02-first-steps/15-function-basics/1-if-else-required/solution.md b/1-js/02-first-steps/15-function-basics/1-if-else-required/solution.md index e41c80418..7e1a36a09 100644 --- a/1-js/02-first-steps/15-function-basics/1-if-else-required/solution.md +++ b/1-js/02-first-steps/15-function-basics/1-if-else-required/solution.md @@ -1 +1 @@ -No difference. \ No newline at end of file +कोई फर्क नहीं। diff --git a/1-js/02-first-steps/15-function-basics/1-if-else-required/task.md b/1-js/02-first-steps/15-function-basics/1-if-else-required/task.md index 4f69a5c8c..3678b4034 100644 --- a/1-js/02-first-steps/15-function-basics/1-if-else-required/task.md +++ b/1-js/02-first-steps/15-function-basics/1-if-else-required/task.md @@ -2,11 +2,11 @@ importance: 4 --- -# Is "else" required? +# क्या "else" आवश्यक है? -The following function returns `true` if the parameter `age` is greater than `18`. +यदि `age` parameter `18` से अधिक है, तो निम्नलिखित function `true` लौटाता है। -Otherwise it asks for a confirmation and returns its result: +अन्यथा, यह एक पुष्टिकरण मांगता है और अपना परिणाम देता है: ```js function checkAge(age) { @@ -15,13 +15,13 @@ function checkAge(age) { *!* } else { // ... - return confirm('Did parents allow you?'); + return confirm('क्या माता-पिता ने आपको अनुमति दी थी?'); } */!* } ``` -Will the function work differently if `else` is removed? +यदि `else` हटा दिया जाता है तो क्या function अलग तरह से काम करेगा? ```js function checkAge(age) { @@ -30,9 +30,9 @@ function checkAge(age) { } *!* // ... - return confirm('Did parents allow you?'); + return confirm('क्या माता-पिता ने आपको अनुमति दी थी?'); */!* } ``` -Is there any difference in the behavior of these two variants? +क्या इन दोनों रूपों के व्यवहार में कोई अंतर है? diff --git a/1-js/02-first-steps/15-function-basics/2-rewrite-function-question-or/solution.md b/1-js/02-first-steps/15-function-basics/2-rewrite-function-question-or/solution.md index c8ee9618f..c212e65b0 100644 --- a/1-js/02-first-steps/15-function-basics/2-rewrite-function-question-or/solution.md +++ b/1-js/02-first-steps/15-function-basics/2-rewrite-function-question-or/solution.md @@ -1,17 +1,17 @@ -Using a question mark operator `'?'`: +प्रश्न चिह्न ऑपरेटर का उपयोग करते हुए `?`: ```js function checkAge(age) { - return (age > 18) ? true : confirm('Did parents allow you?'); + return (age > 18) ? true : confirm('क्या माता-पिता ने आपको अनुमति दी थी?'); } ``` -Using OR `||` (the shortest variant): +OR ऑपरेटर का उपयोग करते हुए `||` (सबसे छोटा संस्करण) ```js function checkAge(age) { - return (age > 18) || confirm('Did parents allow you?'); + return (age > 18) || confirm('क्या माता-पिता ने आपको अनुमति दी थ?'); } ``` -Note that the parentheses around `age > 18` are not required here. They exist for better readabilty. +ध्यान दें कि यहां `age > 18` के आसपास के कोष्ठकों की आवश्यकता नहीं है। वे बेहतर पठनीयता के लिए मौजूद हैं। diff --git a/1-js/02-first-steps/15-function-basics/2-rewrite-function-question-or/task.md b/1-js/02-first-steps/15-function-basics/2-rewrite-function-question-or/task.md index 46da079c0..59d0fc840 100644 --- a/1-js/02-first-steps/15-function-basics/2-rewrite-function-question-or/task.md +++ b/1-js/02-first-steps/15-function-basics/2-rewrite-function-question-or/task.md @@ -2,25 +2,25 @@ importance: 4 --- -# Rewrite the function using '?' or '||' +# '?' या '||' का उपयोग करके function को फिर से लिखें| -The following function returns `true` if the parameter `age` is greater than `18`. +यदि `age` parameter `18` से अधिक है, तो निम्नलिखित function `true` लौटाता है। -Otherwise it asks for a confirmation and returns its result. +अन्यथा, यह एक पुष्टिकरण मांगता है और अपना परिणाम देता है: ```js function checkAge(age) { if (age > 18) { return true; } else { - return confirm('Did parents allow you?'); + return confirm("क्या माता-पिता ने आपको अनुमति दी थी?"); } } ``` -Rewrite it, to perform the same, but without `if`, in a single line. +इस function को फिर से लिखें, लेकिन बिना `if` के, एक पंक्ति में। -Make two variants of `checkAge`: +`checkAge` के दो प्रकार बनाएं: -1. Using a question mark operator `?` -2. Using OR `||` +1. प्रश्न चिह्न ऑपरेटर का उपयोग करते हुए `?` +2. OR ऑपरेटर का उपयोग करते हुए `||` diff --git a/1-js/02-first-steps/15-function-basics/3-min/solution.md b/1-js/02-first-steps/15-function-basics/3-min/solution.md index 2236d9203..e3f1c0d47 100644 --- a/1-js/02-first-steps/15-function-basics/3-min/solution.md +++ b/1-js/02-first-steps/15-function-basics/3-min/solution.md @@ -1,4 +1,4 @@ -A solution using `if`: +`if` का उपयोग करके एक हल: ```js function min(a, b) { @@ -10,7 +10,7 @@ function min(a, b) { } ``` -A solution with a question mark operator `'?'`: +प्रश्न चिह्न ऑपरेटर `'?'` के साथ एक हल: ```js function min(a, b) { @@ -18,4 +18,4 @@ function min(a, b) { } ``` -P.S. In the case of an equality `a == b` it does not matter what to return. \ No newline at end of file +नोट: समानता `a == b` के मामले में यह मायने नहीं रखता कि क्या लौटाया जाए। diff --git a/1-js/02-first-steps/15-function-basics/3-min/task.md b/1-js/02-first-steps/15-function-basics/3-min/task.md index 50edd0d36..56af1f434 100644 --- a/1-js/02-first-steps/15-function-basics/3-min/task.md +++ b/1-js/02-first-steps/15-function-basics/3-min/task.md @@ -4,13 +4,12 @@ importance: 1 # Function min(a, b) -Write a function `min(a,b)` which returns the least of two numbers `a` and `b`. +एक फ़ंक्शन लिखें `min(a,b)` जो `a` और `b` नंबर में से जो सबसे छोटा है उसको लौटाता हो। -For instance: +उदाहरण के लिए: ```js min(2, 5) == 2 min(3, -1) == -1 min(1, 1) == 1 ``` - diff --git a/1-js/02-first-steps/15-function-basics/4-pow/task.md b/1-js/02-first-steps/15-function-basics/4-pow/task.md index f569320c7..af06008b4 100644 --- a/1-js/02-first-steps/15-function-basics/4-pow/task.md +++ b/1-js/02-first-steps/15-function-basics/4-pow/task.md @@ -4,7 +4,7 @@ importance: 4 # Function pow(x,n) -Write a function `pow(x,n)` that returns `x` in power `n`. Or, in other words, multiplies `x` by itself `n` times and returns the result. +एक फ़ंक्शन लिखें `pow(x,n)` जो `x` को `n` की घात में लौटाता है। या, दूसरे शब्दों में, `x` को अपने आप से `n` बार गुणा करता है और परिणाम देता है। ```js pow(3, 2) = 3 * 3 = 9 @@ -12,8 +12,8 @@ pow(3, 3) = 3 * 3 * 3 = 27 pow(1, 100) = 1 * 1 * ...* 1 = 1 ``` -Create a web-page that prompts for `x` and `n`, and then shows the result of `pow(x,n)`. +एक वेब पेज बनाएं जो `x` और `n` के लिए prompt करता है, और फिर `pow(x,n)` का परिणाम दिखाता है। [demo] -P.S. In this task the function should support only natural values of `n`: integers up from `1`. +नोट: इस टास्क में function को केवल `n` के प्राकृतिक मानों को स्वीकार करना चाहिए: `1` से ऊपर के पूर्णांक। diff --git a/1-js/02-first-steps/15-function-basics/article.md b/1-js/02-first-steps/15-function-basics/article.md index b12d0b9e7..9ff176a85 100644 --- a/1-js/02-first-steps/15-function-basics/article.md +++ b/1-js/02-first-steps/15-function-basics/article.md @@ -1,26 +1,26 @@ # Functions -Quite often we need to perform a similar action in many places of the script. +अक्सर हमें स्क्रिप्ट के कई स्थानों पर इसी तरह की क्रिया करने की आवश्यकता होती है। -For example, we need to show a nice-looking message when a visitor logs in, logs out and maybe somewhere else. +उदाहरण के लिए, हमें एक अच्छा दिखने वाला संदेश दिखाना चाहिए जब कोई आगंतुक लॉग इन करता है, लॉग आउट करता है। -Functions are the main "building blocks" of the program. They allow the code to be called many times without repetition. +Functions प्रोग्राम के मुख्य "बिल्डिंग ब्लॉक्स" हैं। वे बिना दोहराव के कोड को कई बार कॉल करने की अनुमति देते हैं। -We've already seen examples of built-in functions, like `alert(message)`, `prompt(message, default)` and `confirm(question)`. But we can create functions of our own as well. +हम पहले से ही बिल्ट-इन function के उदाहरण देख चुके हैं, जैसे `alert(message)`, `prompt(message, default)` और `confirm(question)`। लेकिन हम अपने स्वयं के functions भी बना सकते हैं। -## Function Declaration +## Function घोषणा -To create a function we can use a *function declaration*. +function बनाने के लिए हम एक _function घोषणा_ का उपयोग कर सकते हैं। -It looks like this: +यह इस तरह दिखता है: ```js function showMessage() { - alert( 'Hello everyone!' ); + alert( "सभी को नमस्कार!" ); } ``` -The `function` keyword goes first, then goes the *name of the function*, then a list of *parameters* between the parentheses (comma-separated, empty in the example above) and finally the code of the function, also named "the function body", between curly braces. +`function` कीवर्ड पहले जाता है, फिर _function का नाम_ जाता है, फिर कोष्ठक के बीच _parameters_ की एक सूची (उपरोक्त उदाहरण में खाली) और अंत में function का कोड, जिसे "function बॉडी" भी कहा जाता है, घुंघराले ब्रेसिज़ के बीच। ```js function name(parameters) { @@ -28,13 +28,13 @@ function name(parameters) { } ``` -Our new function can be called by its name: `showMessage()`. +हमारे नए function को इसके नाम से बुलाया जा सकता है: `showMessage()`। -For instance: +उदाहरण के लिए: ```js run function showMessage() { - alert( 'Hello everyone!' ); + alert( 'सभी को नमस्कार!' ); } *!* @@ -43,103 +43,103 @@ showMessage(); */!* ``` -The call `showMessage()` executes the code of the function. Here we will see the message two times. +`showMessage()` कॉल करने से function के कोड को निष्पादित करता है। यहां हम संदेश को दो बार देखेंगे। -This example clearly demonstrates one of the main purposes of functions: to avoid code duplication. +यह उदाहरण स्पष्ट रूप से function के मुख्य उद्देश्यों में से एक को प्रदर्शित करता है: कोड आवृत्ति से बचने के लिए। -If we ever need to change the message or the way it is shown, it's enough to modify the code in one place: the function which outputs it. +यदि हमें कभी भी संदेश या उसके दिखाए जाने के तरीके को बदलने की आवश्यकता होती है, तो यह कोड को एक ही स्थान पर संशोधित करने के लिए पर्याप्त है: वह function जो इसे आउटपुट करता है। -## Local variables +## लोकल variables -A variable declared inside a function is only visible inside that function. +किसी function के अंदर घोषित एक variable केवल उस function के अंदर ही दिखाई देता है। -For example: +उदाहरण के लिए: ```js run function showMessage() { *!* - let message = "Hello, I'm JavaScript!"; // local variable + let message = "हैलो, मैं जावास्क्रिप्ट हूँ!"; // local variable */!* alert( message ); } -showMessage(); // Hello, I'm JavaScript! +showMessage(); // हैलो, मैं जावास्क्रिप्ट हूँ! -alert( message ); // <-- Error! The variable is local to the function +alert( message ); // <-- Error! variable function के लिए स्थानीय है ``` -## Outer variables +## बाहरी variables -A function can access an outer variable as well, for example: +एक function बाहरी variable को भी एक्सेस कर सकता है, उदाहरण के लिए: ```js run no-beautify let *!*userName*/!* = 'John'; function showMessage() { - let message = 'Hello, ' + *!*userName*/!*; + let message = 'नमस्ते, ' + *!*userName*/!*; alert(message); } -showMessage(); // Hello, John +showMessage(); // नमस्ते, John ``` -The function has full access to the outer variable. It can modify it as well. +function के पास बाहरी variable तक पूर्ण पहुंच है। यह इसे बदल भी कर सकता है। -For instance: +उदाहरण के लिए: ```js run let *!*userName*/!* = 'John'; function showMessage() { - *!*userName*/!* = "Bob"; // (1) changed the outer variable + *!*userName*/!* = "Bob"; // (1) बाहरी variable बदल दिया let message = 'Hello, ' + *!*userName*/!*; alert(message); } -alert( userName ); // *!*John*/!* before the function call +alert( userName ); // *!*John*/!* function कॉल से पहले showMessage(); -alert( userName ); // *!*Bob*/!*, the value was modified by the function +alert( userName ); // *!*Bob*/!*, मान function द्वारा संशोधित किया गया था ``` -The outer variable is only used if there's no local one. +बाहरी variable का उपयोग केवल तभी किया जाता है जब कोई लोकल न हो। -If a same-named variable is declared inside the function then it *shadows* the outer one. For instance, in the code below the function uses the local `userName`. The outer one is ignored: +यदि function के अंदर एक ही नामित variable घोषित किया जाता है तो यह बाहरी वाले variable को अनदेखा कर देता है। उदाहरण के लिए, नीचे दिए गए कोड में function लोकल `userName` का उपयोग करता है। बाहरी को अनदेखा किया जाता है: ```js run let userName = 'John'; function showMessage() { *!* - let userName = "Bob"; // declare a local variable + let userName = "Bob"; // एक local variable घोषित करें */!* let message = 'Hello, ' + userName; // *!*Bob*/!* alert(message); } -// the function will create and use its own userName +// function अपना स्वयं का userName बनाएगा और उसका उपयोग करेगा showMessage(); -alert( userName ); // *!*John*/!*, unchanged, the function did not access the outer variable +alert( userName ); // *!*John*/!*, अपरिवर्तित, function बाहरी variable का उपयोग नहीं करता है ``` ```smart header="Global variables" -Variables declared outside of any function, such as the outer `userName` in the code above, are called *global*. +किसी function के बाहर घोषित variables, जैसे उपरोक्त कोड में बाहरी `userName`, *ग्लोबल* कहलाते हैं। -Global variables are visible from any function (unless shadowed by locals). +ग्लोबल variables किसी भी function से दिखाई देते हैं (जब तक कि लोकल द्वारा छिपा न हो)। -It's a good practice to minimize the use of global variables. Modern code has few or no globals. Most variables reside in their functions. Sometimes though, they can be useful to store project-level data. +ग्लोबल variables के उपयोग को कम करना अच्छा अभ्यास है। आधुनिक कोड में कुछ या कोई ग्लोबल्स नहीं हैं। अधिकांश variable अपने functions में रहते हैं। हालांकि कभी-कभी, वे प्रोजेक्ट-स्तरीय डेटा संग्रहीत करने के लिए उपयोगी हो सकते हैं। ``` ## Parameters -We can pass arbitrary data to functions using parameters (also called *function arguments*) . +हम Parameters (जिसे _function arguments_ भी कहा जाता है) का उपयोग करके function को मनमाना डेटा पास कर सकते हैं। -In the example below, the function has two parameters: `from` and `text`. +नीचे दिए गए उदाहरण में, function के दो parameters हैं: `from` और `text`। ```js run function showMessage(*!*from, text*/!*) { // arguments: from, text @@ -152,10 +152,9 @@ showMessage('Ann', "What's up?"); // Ann: What's up? (**) */!* ``` -When the function is called in lines `(*)` and `(**)`, the given values are copied to local variables `from` and `text`. Then the function uses them. - -Here's one more example: we have a variable `from` and pass it to the function. Please note: the function changes `from`, but the change is not seen outside, because a function always gets a copy of the value: +जब function को लाइनों में कॉल किया जाता है `(*)` और `(**)`, तो दिए गए मानों को लोकल variables में कॉपी किया जाता है `from` और `text`। फिर function उनका उपयोग करता है। +यहां एक और उदाहरण दिया गया है: हमारे पास एक variable `from` है और इसे function में पास करें। कृपया ध्यान दें: function `from` को बदलता है, लेकिन परिवर्तन बाहर नहीं देखा जाता है, क्योंकि function को हमेशा मूल्य की एक प्रति प्राप्त होती है: ```js run function showMessage(from, text) { @@ -175,19 +174,19 @@ showMessage(from, "Hello"); // *Ann*: Hello alert( from ); // Ann ``` -## Default values +## डिफॉल्ट वैल्यूज -If a parameter is not provided, then its value becomes `undefined`. +यदि कोई parameter प्रदान नहीं किया जाता है, तो उसका मान `undefined` हो जाता है। -For instance, the aforementioned function `showMessage(from, text)` can be called with a single argument: +उदाहरण के लिए, उपरोक्त function `showMessage(from, text)` को एक argument के साथ बुलाया जा सकता है: ```js showMessage("Ann"); ``` -That's not an error. Such a call would output `"*Ann*: undefined"`. There's no `text`, so it's assumed that `text === undefined`. +यह कोई एरर नहीं है। ऐसी कॉल `"*Ann*: undefined"` आउटपुट करेगी। कोई `text` नहीं है, इसलिए यह माना जाता है कि `text === undefined`। -If we want to use a "default" `text` in this case, then we can specify it after `=`: +यदि हम इस मामले में "डिफ़ॉल्ट" `text` का उपयोग करना चाहते हैं, तो हम इसे `=` के बाद उल्लिखत कर सकते हैं: ```js run function showMessage(from, *!*text = "no text given"*/!*) { @@ -197,9 +196,9 @@ function showMessage(from, *!*text = "no text given"*/!*) { showMessage("Ann"); // Ann: no text given ``` -Now if the `text` parameter is not passed, it will get the value `"no text given"` +अब यदि `text` parameter नहीं देना गया हो, तो उसे `"no text given"` मान मिलेगा। -Here `"no text given"` is a string, but it can be a more complex expression, which is only evaluated and assigned if the parameter is missing. So, this is also possible: +यहां `"no text given"` एक स्ट्रिंग है, लेकिन यह एक अधिक जटिल अभिव्यक्ति हो सकती है, जिसका केवल मूल्यांकन किया जाता है और parameter गायब होने पर असाइन किया जाता है। तो, यह भी संभव है: ```js run function showMessage(from, text = anotherFunction()) { @@ -209,45 +208,45 @@ function showMessage(from, text = anotherFunction()) { ``` ```smart header="Evaluation of default parameters" -In JavaScript, a default parameter is evaluated every time the function is called without the respective parameter. +जावास्क्रिप्ट में, हर बार संबंधित parameter के बिना function को कॉल किए जाने पर एक डिफ़ॉल्ट parameter का मूल्यांकन किया जाता है। -In the example above, `anotherFunction()` is called every time `showMessage()` is called without the `text` parameter. +ऊपर दिए गए उदाहरण में, `anotherFunction()` को हर बार कॉल किया जाता है जब भी `showMessage()` को `text` parameter के बिना कॉल किया जाता है। ``` -### Alternative default parameters +### वैकल्पिक डिफ़ॉल्ट parameters -Sometimes it makes sense to set default values for parameters not in the function declaration, but at a later stage, during its execution. +कभी-कभी function घोषणा में parameter के लिए डिफ़ॉल्ट मान सेट करना समझ में आता है, लेकिन बाद के चरण में, इसके निष्पादन के दौरान भी सेट किया जाता है। -To check for an omitted parameter, we can compare it with `undefined`: +छोड़े गए parameter की जांच करने के लिए, हम इसकी तुलना `undefined` से कर सकते हैं: ```js run function showMessage(text) { *!* if (text === undefined) { - text = 'empty message'; + text = 'खाली संदेश'; } */!* alert(text); } -showMessage(); // empty message +showMessage(); // खाली संदेश ``` -...Or we could use the `||` operator: +...या हम `||` ऑपरेटर का उपयोग कर सकते हैं: ```js -// if text parameter is omitted or "" is passed, set it to 'empty' +// यदि टेक्स्ट parameter छोड़ा गया है या "" पास किया गया है, तो इसे 'empty' पर सेट करें function showMessage(text) { text = text || 'empty'; ... } ``` -Modern JavaScript engines support the [nullish coalescing operator](info:nullish-coalescing-operator) `??`, it's better when falsy values, such as `0`, are considered regular: +आधुनिक जावास्क्रिप्ट इंजन [nullish coalescing operator](info:nullish-coalescing-operator) `??` को सपोर्ट करती है| यह तब बेहतर होता है जब `0` जैसे झूठे मूल्यों को नियमित माना जाता है: ```js run -// if there's no "count" parameter, show "unknown" +// यदि कोई "count" parameter नहीं है, तो "unknown" दिखाएं function showCount(count) { alert(count ?? "unknown"); } @@ -257,11 +256,11 @@ showCount(null); // unknown showCount(); // unknown ``` -## Returning a value +## एक मान लौटाना -A function can return a value back into the calling code as the result. +एक function परिणाम के रूप में एक मान वापस कॉलिंग कोड में लौटा सकता है। -The simplest example would be a function that sums two values: +सबसे सरल उदाहरण एक ऐसा function होगा जो दो मानों का योग करता है: ```js run no-beautify function sum(a, b) { @@ -272,9 +271,9 @@ let result = sum(1, 2); alert( result ); // 3 ``` -The directive `return` can be in any place of the function. When the execution reaches it, the function stops, and the value is returned to the calling code (assigned to `result` above). +निर्देश `return` function के किसी भी स्थान पर हो सकता है। जब निष्पादन उस तक पहुंच जाता है, तो function बंद हो जाता है, और मान कॉलिंग कोड पर वापस आ जाता है (उपरोक्त `result` को सौंपा गया) -There may be many occurrences of `return` in a single function. For instance: +एक function में `return` की कई घटनाएं हो सकती हैं। उदाहरण के लिए: ```js run function checkAge(age) { @@ -284,23 +283,23 @@ function checkAge(age) { */!* } else { *!* - return confirm('Do you have permission from your parents?'); + return confirm('क्या आपके पास अपने माता-पिता की अनुमति है?'); */!* } } -let age = prompt('How old are you?', 18); +let age = prompt('आपकी उम्र क्या है?', 18); if ( checkAge(age) ) { - alert( 'Access granted' ); + alert( 'प्रवेश करने की अनुमति है' ); } else { - alert( 'Access denied' ); + alert( 'पहुंच अस्वीकृत' ); } ``` -It is possible to use `return` without a value. That causes the function to exit immediately. +बिना मूल्य के `return` का उपयोग करना संभव है। यह function को तुरंत बाहर निकलने का कारण बनता है। -For example: +उदाहरण के लिए: ```js function showMovie(age) { @@ -310,23 +309,22 @@ function showMovie(age) { */!* } - alert( "Showing you the movie" ); // (*) + alert( "आपको फिल्म दिखा रहा है" ); // (*) // ... } ``` -In the code above, if `checkAge(age)` returns `false`, then `showMovie` won't proceed to the `alert`. +ऊपर दिए गए कोड में, अगर `checkAge(age)` `false` लौटाता है, तो `showMovie` `alert` पर आगे नहीं बढ़ेगा। -````smart header="A function with an empty `return` or without it returns `undefined`" -If a function does not return a value, it is the same as if it returns `undefined`: +````smart header="A function with an empty `return`or without it returns`undefined`" If a function does not return a value, it is the same as if it returns `undefined`: ```js run -function doNothing() { /* empty */ } +function doNothing() { /* खाली */ } alert( doNothing() === undefined ); // true ``` -An empty `return` is also the same as `return undefined`: +एक खाली `return` भी `return undefined` जैसा ही है: ```js run function doNothing() { @@ -338,22 +336,22 @@ alert( doNothing() === undefined ); // true ```` ````warn header="Never add a newline between `return` and the value" -For a long expression in `return`, it might be tempting to put it on a separate line, like this: +`return` में एक लंबी अभिव्यक्ति के लिए, इसे एक अलग लाइन पर रखना आकर्षक हो सकता है, जैसे: ```js return (some + long + expression + or + whatever * f(a) + f(b)) ``` -That doesn't work, because JavaScript assumes a semicolon after `return`. That'll work the same as: +यह काम नहीं करता है, क्योंकि `return` के बाद जावास्क्रिप्ट सेमीकोलन लगा देता है। यह उसी तरह काम करेगा: ```js return*!*;*/!* (some + long + expression + or + whatever * f(a) + f(b)) ``` -So, it effectively becomes an empty return. +तो, यह प्रभावी रूप से एक खाली रिटर्न बन जाता है। -If we want the returned expression to wrap across multiple lines, we should start it at the same line as `return`. Or at least put the opening parentheses there as follows: +यदि हम चाहते हैं कि लौटाए गए एक्सप्रेशन को कई लाइनों में लपेटा जाए, तो हमें इसे उसी लाइन से शुरू करना चाहिए जैसे `return`। या कम से कम उद्घाटन कोष्ठक इस प्रकार रखें: ```js return ( @@ -362,82 +360,81 @@ return ( whatever * f(a) + f(b) ) ``` -And it will work just as we expect it to. +और यह वैसे ही काम करेगा जैसा हम उम्मीद करते हैं। ```` -## Naming a function [#function-naming] +## Function का नामकरण [#function-naming] -Functions are actions. So their name is usually a verb. It should be brief, as accurate as possible and describe what the function does, so that someone reading the code gets an indication of what the function does. +Functions क्रिया हैं। तो उनका नाम आमतौर पर एक क्रिया है। यह संक्षिप्त होना चाहिए, यथासंभव सटीक होना चाहिए और यह वर्णन करना चाहिए कि function क्या करता है, ताकि कोड पढ़ने वाले व्यक्ति को यह संकेत मिल सके कि function क्या करता है। -It is a widespread practice to start a function with a verbal prefix which vaguely describes the action. There must be an agreement within the team on the meaning of the prefixes. +क्रिया को अस्पष्ट रूप से वर्णित करने वाले मौखिक उपसर्ग के साथ function प्रारंभ करना एक व्यापक प्रथा है। उपसर्गों के अर्थ पर टीम के भीतर एक समझौता होना चाहिए। -For instance, functions that start with `"show"` usually show something. +उदाहरण के लिए, `"show"` से शुरू होने वाले function आमतौर पर कुछ दिखाते हैं। -Function starting with... +Function की शुरुआत... -- `"get…"` -- return a value, -- `"calc…"` -- calculate something, -- `"create…"` -- create something, -- `"check…"` -- check something and return a boolean, etc. +- `"get…"` -- एक मूल्य वापस करें, +- `"calc…"` -- कुछ गणना करो, +- `"create…"` -- कुछ बनाये, +- `"check…"` -- कुछ जांचें और एक बूलियन लौटाएं, आदि। -Examples of such names: +ऐसे नामों के उदाहरण: ```js no-beautify -showMessage(..) // shows a message -getAge(..) // returns the age (gets it somehow) -calcSum(..) // calculates a sum and returns the result -createForm(..) // creates a form (and usually returns it) -checkPermission(..) // checks a permission, returns true/false +showMessage(..) // एक संदेश दिखाता है +getAge(..) // उम्र लौटाता है (इसे किसी तरह प्राप्त करता है) +calcSum(..) // एक योग की गणना करता है और परिणाम देता है +createForm(..) // एक फॉर्म बनाता है (और आमतौर पर इसे वापस करता है) +checkPermission(..) // अनुमति की जाँच करता है, सही/गलत लौटाता है ``` -With prefixes in place, a glance at a function name gives an understanding what kind of work it does and what kind of value it returns. +उपसर्गों के साथ, function नाम पर एक नज़र यह समझ देती है कि यह किस प्रकार का कार्य करता है और यह किस प्रकार का मूल्य देता है। ```smart header="One function -- one action" -A function should do exactly what is suggested by its name, no more. +एक function को ठीक वही करना चाहिए जो उसके नाम से सुझाया गया है, और कुछ नहीं। -Two independent actions usually deserve two functions, even if they are usually called together (in that case we can make a 3rd function that calls those two). +दो स्वतंत्र क्रियाएं आम तौर पर दो functions की सेवा करती हैं, भले ही उन्हें आम तौर पर एक साथ बुलाया जाता है (उस स्थिति में हम एक तीसरा function बना सकते हैं जो उन दोनों को कॉल करता है)। -A few examples of breaking this rule: +इस नियम को तोड़ने के कुछ उदाहरण: -- `getAge` -- would be bad if it shows an `alert` with the age (should only get). -- `createForm` -- would be bad if it modifies the document, adding a form to it (should only create it and return). -- `checkPermission` -- would be bad if it displays the `access granted/denied` message (should only perform the check and return the result). +- `getAge` -- बुरा होगा अगर यह उम्र के साथ एक `alert` दिखाता है (सिर्फ उम्र लेना चाहिए)। +- `createForm` -- खराब होगा यदि यह डॉक्यूमेंट को संशोधित करता है, इसमें एक फॉर्म जोड़ता है (केवल इसे बनाना और वापस करना चाहिए)। +- `checkPermission` -- खराब होगा यदि यह `access granted/denied` संदेश प्रदर्शित करता है (केवल जांच करना चाहिए और परिणाम वापस करना चाहिए)। -These examples assume common meanings of prefixes. You and your team are free to agree on other meanings, but usually they're not much different. In any case, you should have a firm understanding of what a prefix means, what a prefixed function can and cannot do. All same-prefixed functions should obey the rules. And the team should share the knowledge. +ये उदाहरण उपसर्गों के सामान्य अर्थ ग्रहण करते हैं। आप और आपकी टीम अन्य अर्थों पर सहमत होने के लिए स्वतंत्र हैं, लेकिन आमतौर पर, वे बहुत भिन्न नहीं होते हैं। किसी भी मामले में, आपको इस बात की पक्की समझ होनी चाहिए कि उपसर्ग का क्या अर्थ है, उपसर्ग का function क्या कर सकता है और क्या नहीं। सभी समान-उपसर्ग function को नियमों का पालन करना चाहिए। और टीम को ज्ञान साझा करना चाहिए। ``` ```smart header="Ultrashort function names" -Functions that are used *very often* sometimes have ultrashort names. +*अक्सर* उपयोग किए जाने वाले functions में कभी-कभी अल्ट्रा-शॉर्ट नाम होते हैं। -For example, the [jQuery](http://jquery.com) framework defines a function with `$`. The [Lodash](http://lodash.com/) library has its core function named `_`. +उदाहरण के लिए, [jQuery](http://jquery.com) फ्रेमवर्क `$` के साथ एक function को परिभाषित करता है। The [Lodash](http://lodash.com/) library का मुख्य function `_` है। -These are exceptions. Generally functions names should be concise and descriptive. +ये अपवाद हैं। आम तौर पर functions नाम संक्षिप्त और वर्णनात्मक होने चाहिए। ``` ## Functions == Comments -Functions should be short and do exactly one thing. If that thing is big, maybe it's worth it to split the function into a few smaller functions. Sometimes following this rule may not be that easy, but it's definitely a good thing. +Functions छोटे होने चाहिए और ठीक एक काम करना चाहिए। अगर वह चीज बड़ी है, तो शायद यह function को कुछ छोटे functions में विभाजित करना बेहतर है। कभी-कभी इस नियम का पालन करना इतना आसान नहीं हो सकता है, लेकिन यह निश्चित रूप से एक अच्छी बात है। -A separate function is not only easier to test and debug -- its very existence is a great comment! +एक अलग function न केवल परीक्षण और डीबग करना आसान है - इसका अस्तित्व एक महान comment है! -For instance, compare the two functions `showPrimes(n)` below. Each one outputs [prime numbers](https://en.wikipedia.org/wiki/Prime_number) up to `n`. +उदाहरण के लिए, नीचे दिए गए दो functions `showPrimes(n)` की तुलना करें। हर एक `n` तक [अभाज्य संख्याएँ](https://en.wikipedia.org/wiki/Prime_number) आउटपुट करता है। -The first variant uses a label: +पहला संस्करण एक लेबल का उपयोग करता है: ```js function showPrimes(n) { nextPrime: for (let i = 2; i < n; i++) { - for (let j = 2; j < i; j++) { if (i % j == 0) continue nextPrime; } - alert( i ); // a prime + alert( i ); // एक अभाज्य संख्या } } ``` -The second variant uses an additional function `isPrime(n)` to test for primality: +दूसरा संस्करण अभाज्यता के परीक्षण के लिए एक अतिरिक्त function `isPrime(n)` का उपयोग करता है: ```js function showPrimes(n) { @@ -445,7 +442,7 @@ function showPrimes(n) { for (let i = 2; i < n; i++) { *!*if (!isPrime(i)) continue;*/!* - alert(i); // a prime + alert(i); // एक अभाज्य संख्या } } @@ -457,13 +454,13 @@ function isPrime(n) { } ``` -The second variant is easier to understand, isn't it? Instead of the code piece we see a name of the action (`isPrime`). Sometimes people refer to such code as *self-describing*. +दूसरा संस्करण समझना आसान है, है ना? कोड पीस के बजाय हम क्रिया का एक नाम देखते हैं (`isPrime`)। कभी-कभी लोग ऐसे कोड को _स्वयं का वर्णन करना_ कहते हैं। -So, functions can be created even if we don't intend to reuse them. They structure the code and make it readable. +इसलिए, function बनाए जा सकते हैं, भले ही हम उनका पुन: उपयोग करने का इरादा न रखते हों। वे कोड की संरचना करते हैं और इसे पठनीय बनाते हैं। -## Summary +## सारांश -A function declaration looks like this: +एक function घोषणा इस तरह दिखती है: ```js function name(parameters, delimited, by, comma) { @@ -471,18 +468,18 @@ function name(parameters, delimited, by, comma) { } ``` -- Values passed to a function as parameters are copied to its local variables. -- A function may access outer variables. But it works only from inside out. The code outside of the function doesn't see its local variables. -- A function can return a value. If it doesn't, then its result is `undefined`. +- किसी function को दिए गए मान parameters के रूप में उसके स्थानीय variables में कॉपी किए जाते हैं। +- एक function बाहरी variables का इस्तेमाल कर सकता है। लेकिन यह केवल अंदर से बाहर काम करता है। function के बाहर का कोड इसके स्थानीय variables नहीं देखता है। +- एक function एक मान वापस कर सकता है। यदि ऐसा नहीं होता है, तो इसका परिणाम `undefined` होता है। -To make the code clean and easy to understand, it's recommended to use mainly local variables and parameters in the function, not outer variables. +कोड को साफ और समझने में आसान बनाने के लिए, function में मुख्य रूप से स्थानीय variables और parameter का उपयोग करने की अनुशंसा की जाती है, बाहरी variables नहीं। -It is always easier to understand a function which gets parameters, works with them and returns a result than a function which gets no parameters, but modifies outer variables as a side-effect. +ऐसे function को समझना हमेशा आसान होता है जो parameter प्राप्त करता है, उनके साथ काम करता है, और किसी ऐसे function की तुलना में परिणाम देता है जिसमें कोई parameter नहीं होता है, लेकिन बाहरी variables को साइड-इफेक्ट के रूप में संशोधित करता है। -Function naming: +Function नामकरण: -- A name should clearly describe what the function does. When we see a function call in the code, a good name instantly gives us an understanding what it does and returns. -- A function is an action, so function names are usually verbal. -- There exist many well-known function prefixes like `create…`, `show…`, `get…`, `check…` and so on. Use them to hint what a function does. +- एक नाम स्पष्ट रूप से वर्णन करना चाहिए कि function क्या करता है। जब हम कोड में एक function कॉल देखते हैं, तो एक अच्छा नाम हमें तुरंत समझा देता है कि यह क्या करता है और क्या वापस लौटता है। +- एक function एक क्रिया है, इसलिए function नाम आमतौर पर मौखिक होते हैं। +- कई प्रसिद्ध function प्रीफ़िक्स मौजूद हैं जैसे `create…`, `show…`, `get…`, `check…` और इसी तरह। function क्या करता है यह संकेत देने के लिए उनका उपयोग करें। -Functions are the main building blocks of scripts. Now we've covered the basics, so we actually can start creating and using them. But that's only the beginning of the path. We are going to return to them many times, going more deeply into their advanced features. +Function स्क्रिप्ट के मुख्य निर्माण खंड हैं। अब हमने मूल बातें शामिल कर ली हैं, इसलिए हम वास्तव में उन्हें बनाना और उनका उपयोग करना शुरू कर सकते हैं। लेकिन यह सिर्फ रास्ते की शुरुआत है। हम कई बार उनके पास वापस जायेंगे, उन्नत सुविधाओं में और अधिक गहराई से इनको जानेंगे।