Skip to content

Code structure #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 66 additions & 66 deletions 1-js/02-first-steps/02-structure/article.md
Original file line number Diff line number Diff line change
@@ -1,159 +1,159 @@
# Code structure
# कोड संरचना

The first thing we'll study is the building blocks of code.
पहली चीज जो हम अध्ययन करेंगे, वह है कोड की नींव की ईंटें (building blocks)।

## Statements
## उक्ति (Statements) [#उक्ति]

Statements are syntax constructs and commands that perform actions.
उक्ति कार्य करने वाले वाक्य रचना और आदेश हैं।

We've already seen a statement, `alert('Hello, world!')`, which shows the message "Hello, world!".
हमने पहले ही एक उक्ति देखा है, `alert('हेलो विश्व!')`, "हेलो विश्व" जो संदेश दिखाता है।

We can have as many statements in our code as we want. Statements can be separated with a semicolon.
हम अपने कोड में जितने चाहें उतने उक्ति दे सकते हैं। उक्ति अर्धविराम से अलग किए जा सकते हैं।

For example, here we split "Hello World" into two alerts:
उदाहरण के लिए, यहां हमने "हेलो विश्व!" को दो अलर्ट में विभाजित किया है:

```js run no-beautify
alert('Hello'); alert('World');
alert('हेलो!'); alert('विश्व');
```

Usually, statements are written on separate lines to make the code more readable:
आमतौर पर कोड को अधिक पठनीय बनाने के लिए अलग-अलग लाइनों पर उक्ति लिखे जाते हैं:

```js run no-beautify
alert('Hello');
alert('World');
alert('हेलो');
alert('विश्व');
```

## Semicolons [#semicolon]
## अर्धविराम [#अर्धविराम]

A semicolon may be omitted in most cases when a line break exists.
लाइन ब्रेक मौजूद होने पर अधिकांश मामलों में अर्धविराम छोड़ा जा सकता है।

This would also work:
यह कोड काम करेगा:

```js run no-beautify
alert('Hello')
alert('World')
alert('हेलो')
alert('विश्व')
```

Here, JavaScript interprets the line break as an "implicit" semicolon. This is called an [automatic semicolon insertion](https://tc39.github.io/ecma262/#sec-automatic-semicolon-insertion).
यहाँ, जावास्क्रिप्ट लाइन ब्रेक (एक पंक्ति का अंत) को "निहित" अर्धविराम के रूप में मानता है। इसे [automatic semicolon insertion](https://tc39.github.io/ecma262/#sec-automatic-semicolon-insertion) कहा जाता है।

**In most cases, a newline implies a semicolon. But "in most cases" does not mean "always"!**
**ज्यादातर मामलों में, एक नई पंक्ति का अर्थ अर्धविराम है। लेकिन "ज्यादातर मामलों में" का अर्थ "हमेशा" नहीं होता है!**

There are cases when a newline does not mean a semicolon. For example:
ऐसे मामले हैं जब एक नई लाइन का अर्थ अर्धविराम नहीं है। उदाहरण के लिए:

```js run no-beautify
alert(3 +
1
+ 2);
```

The code outputs `6` because JavaScript does not insert semicolons here. It is intuitively obvious that if the line ends with a plus `"+"`, then it is an "incomplete expression", so the semicolon is not required. And in this case that works as intended.
कोड `6` आउटपुट करता है क्योंकि जावास्क्रिप्ट यहाँ अर्धविराम नहीं डालता है। यह सहज रूप से स्पष्ट है कि यदि उक्ति एक प्लस `" + "` के साथ समाप्त होता है, तो यह एक "अपूर्ण उक्ति" है, इसलिए अर्धविराम की आवश्यकता नहीं है। और इस मामले में जो इरादा के अनुसार काम करता है।

**But there are situations where JavaScript "fails" to assume a semicolon where it is really needed.**
**लेकिन ऐसी परिस्थितियां भी हैं जहां जावास्क्रिप्ट अर्धविराम लगाने के लिए "विफल" होता है, जहां इसकी वास्तव में आवश्यकता होती है।**

Errors which occur in such cases are quite hard to find and fix.
ऐसे मामलों में होने वाली त्रुटियां खोजने और ठीक करने में काफी कठिन हैं।

````smart header="An example of an error"
If you're curious to see a concrete example of such an error, check this code out:
````smart header="एक त्रुटि का उदाहरण"
यदि आप ऐसी त्रुटि का ठोस उदाहरण देखने के लिए उत्सुक हैं, तो इस कोड को देखें:

```js run
[1, 2].forEach(alert)
```

No need to think about the meaning of the brackets `[]` and `forEach` yet. We'll study them later. For now, just remember the result of the code: it shows `1` then `2`.
`[]` और 'forEach` के अर्थ के बारे में सोचने की आवश्यकता नहीं है। हम उनका अध्ययन बाद में करेंगे। अभी के लिए, बस कोड का परिणाम याद रखें: यह `1` दिखाता है फिर` 2`।

Now, let's add an `alert` before the code and *not* finish it with a semicolon:
अब, कोड से पहले एक `अलर्ट` जोड़ें और अर्धविराम के साथ इसे *समाप्त न करें*:

```js run no-beautify
alert("There will be an error")
alert("इस कोड में एक त्रुटि होगी")

[1, 2].forEach(alert)
```

Now if we run the code, only the first `alert` is shown and then we have an error!
अब यदि हम कोड चलाते हैं, तो केवल पहले `alert` दिखाया जाता है और फिर एक त्रुटि होती है!

But everything is fine again if we add a semicolon after `alert`:
लेकिन सब कुछ फिर से ठीक है अगर हम `alert` के बाद एक अर्धविराम जोड़ते हैं:
```js run
alert("All fine now");
alert("अब सब ठीक है");

[1, 2].forEach(alert)
```

Now we have the "All fine now" message followed by `1` and `2`.
अब हमें "अब सब ठीक है" संदेश मिलता है और उसके बाद `1` और` 2` मिलते हैं।


The error in the no-semicolon variant occurs because JavaScript does not assume a semicolon before square brackets `[...]`.
बिना अर्धविराम के संस्करण में त्रुटि होती है क्योंकि `[...]` से पहले जावास्क्रिप्ट अर्धविराम नहीं मानता है।

So, because the semicolon is not auto-inserted, the code in the first example is treated as a single statement. Here's how the engine sees it:
इसलिए, क्योंकि अर्धविराम को ऑटो-सम्मिलित नहीं किया गया है, पहले उदाहरण में कोड को एकल उक्ति माना जाता है। यहां बताया गया है कि इंजन इसे कैसे देखता है:

```js run no-beautify
alert("There will be an error")[1, 2].forEach(alert)
alert("इस कोड में एक त्रुटि होग")[1, 2].forEach(alert)
```

But it should be two separate statements, not one. Such a merging in this case is just wrong, hence the error. This can happen in other situations.
लेकिन यह दो अलग-अलग कथन होने चाहिए, एक नहीं। इस मामले में ऐसा विलय सिर्फ गलत है, इसलिए त्रुटि होती है। यह अन्य स्थितियों में हो सकता है।
````

We recommend putting semicolons between statements even if they are separated by newlines. This rule is widely adopted by the community. Let's note once again -- *it is possible* to leave out semicolons most of the time. But it's safer -- especially for a beginner -- to use them.
हम बयानों के बीच अर्धविराम लगाने की सलाह देते हैं, भले ही वे नई लाइनों से अलग हों। इस नियम को समुदाय द्वारा व्यापक रूप से अपनाया जाता है। आइए एक बार फिर ध्यान दें -- *यह संभव है* अधिकांश समय अर्धविरामों को छोड़ने के लिए। लेकिन यह सुरक्षित है -- विशेष रूप से नौसिखिए के लिए -- अर्धविराम का उपयोग करने के लिए।

## Comments [#code-comments]
## टिप्पणियाँ [#टिप्पणियाँ]

As time goes on, programs become more and more complex. It becomes necessary to add *comments* which describe what the code does and why.
जैसे-जैसे समय बीतता है, कोड अधिक से अधिक जटिल होते जाते हैं। यह आवश्यक है कि *टिप्पणियों* को जोड़ा जाए जो यह बताता है कि कोड क्या करता है और क्यों।

Comments can be put into any place of a script. They don't affect its execution because the engine simply ignores them.
टिप्पणियाँ किसी स्क्रिप्ट के किसी भी स्थान पर डाली जा सकती हैं। वे इसके निष्पादन को प्रभावित नहीं करते हैं क्योंकि इंजन उन्हें अनदेखा करता है।

**One-line comments start with two forward slash characters `//`.**
**एक-पंक्ति टिप्पणियाँ दो फ़ॉरवर्ड स्लैश वर्णों से शुरू होती हैं `//`.**

The rest of the line is a comment. It may occupy a full line of its own or follow a statement.
शेष पंक्ति एक टिप्पणी है। यह अपनी खुद की एक पूरी लाइन पर कब्जा कर सकता है या एक उक्ति के पीछे आ सकता है.

Like here:
जैसे यहाँ:
```js run
// This comment occupies a line of its own
alert('Hello');
// यह टिप्पणी स्वयं की एक लाइन पर है
alert('हैलो');

alert('World'); // This comment follows the statement
alert('विश्व'); // यह टिप्पणी उक्ति के बाद आई है
```

**Multiline comments start with a forward slash and an asterisk <code>/&#42;</code> and end with an asterisk and a forward slash <code>&#42;/</code>.**
**बहुस्तरीय टिप्पणियां आगे की स्लैश और तारांकन के साथ शुरू होती हैं <code>/&#42;</code> और एक तारांकन और एक आगे की स्लैश के साथ समाप्त होती हैं <code>&#42;/</code>.**

Like this:
जैसे यहाँ:

```js run
/* An example with two messages.
This is a multiline comment.
/* दो संदेशों के साथ एक उदाहरण।
यह एक बहुस्तरीय टिप्पणी है।
*/
alert('Hello');
alert('World');
alert('हैलो');
alert('विश्व');
```

The content of comments is ignored, so if we put code inside <code>/&#42; ... &#42;/</code>, it won't execute.
टिप्पणियों में कोड को अनदेखा किया जाता है, इसलिए यदि हम कोड को अंदर रखते हैं, <code>/&#42; ... &#42;/</code>, यह निष्पादित नहीं होगा।

Sometimes it can be handy to temporarily disable a part of code:
कभी-कभी इस तकनीक का उपयोग करके कोड के एक भाग को अस्थायी रूप से अक्षम करना आसान होता है

```js run
/* Commenting out the code
alert('Hello');
/* कोड टिप्पणी की गई है
alert('हैलो');
*/
alert('World');
alert('विश्व');
```

```smart header="Use hotkeys!"
In most editors, a line of code can be commented out by pressing the `key:Ctrl+/` hotkey for a single-line comment and something like `key:Ctrl+Shift+/` -- for multiline comments (select a piece of code and press the hotkey). For Mac, try `key:Cmd` instead of `key:Ctrl` and `key:Option` instead of `key:Shift`.
```smart header="हॉट कुंजी का उपयोग करें!"
अधिकांश संपादकों में, कोड की एक पंक्ति को एक-लाइन टिप्पणी के लिए `key: Ctrl + /` को दबाकर टिप्पणी की जा सकती है और बहुस्तरीय टिप्पणियों के लिए `key: Ctrl + Shift + /` (कोड का एक टुकड़ा चुनें और हॉटकी दबाएं)। Mac के लिए, `key: Ctrl` के बजाय `key: Cmd` और `key: Shift` के बजाय `key: Option` आज़माएं।
```

````warn header="Nested comments are not supported!"
There may not be `/*...*/` inside another `/*...*/`.
````warn header="टिप्पणियाँ जो अन्य टिप्पणियों के अंदर हैं समर्थित नहीं हैं!"
एक `/*...*/` के अंदर `/*...*/` नहीं हो सकता है

Such code will die with an error:
इस तरह के कोड में त्रुटि होगी:

```js run no-beautify
/*
/* nested comment ?!? */
/* नेस्टेड टिप्पणी ?!? */
*/
alert( 'World' );
alert( 'विश्व' );
```
````

Please, don't hesitate to comment your code.
कृपया, अपने कोड में टिप्पणी करने में संकोच न करें।

Comments increase the overall code footprint, but that's not a problem at all. There are many tools which minify code before publishing to a production server. They remove comments, so they don't appear in the working scripts. Therefore, comments do not have negative effects on production at all.
टिप्पणियाँ कोड के समग्र आकार को बढ़ाती हैं, लेकिन यह बिल्कुल समस्या नहीं है। कई उपकरण हैं जो सर्वर पर प्रकाशित होने से पहले कोड को छोटा करते हैं। वे टिप्पणियों को हटा देते हैं, इसलिए वे काम करने वाली स्क्रिप्ट में दिखाई नहीं देते हैं। इसलिए, टिप्पणियों का अंतिम उत्पादन (production) पर नकारात्मक प्रभाव नहीं पड़ता है।

Later in the tutorial there will be a chapter <info:code-quality> that also explains how to write better comments.
बाद में ट्यूटोरियल में एक अध्याय होगा <info:code-quality> जो यह भी बताता है कि बेहतर टिप्पणी कैसे लिखनी है।