diff --git a/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/solution.md b/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/solution.md index 8869d32e6..eaf079387 100644 --- a/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/solution.md +++ b/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/solution.md @@ -1,4 +1,4 @@ -The answer is `2`, that's the first truthy value. +جواب `2` است، آن اولین مقدار truthy است. ```js run alert( null || 2 || undefined ); diff --git a/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/task.md b/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/task.md index a7c9addfc..ae6762875 100644 --- a/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/task.md +++ b/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/task.md @@ -2,9 +2,9 @@ importance: 5 --- -# What's the result of OR? +# نتیجه OR چیست؟ -What is the code below going to output? +کد پایین چه چیزی را خروجی خواهد داد؟ ```js alert( null || 2 || undefined ); diff --git a/1-js/02-first-steps/11-logical-operators/2-alert-or/solution.md b/1-js/02-first-steps/11-logical-operators/2-alert-or/solution.md index f85b56366..46353d1d7 100644 --- a/1-js/02-first-steps/11-logical-operators/2-alert-or/solution.md +++ b/1-js/02-first-steps/11-logical-operators/2-alert-or/solution.md @@ -1,13 +1,13 @@ -The answer: first `1`, then `2`. +جواب: اول `1`، سپس `2`. ```js run alert( alert(1) || 2 || alert(3) ); ``` -The call to `alert` does not return a value. Or, in other words, it returns `undefined`. +صدا زدن `alert` مقداری بر نمی گرداند. یا، به عبارتی دیگر، `undefined` را بر می گرداند. -1. The first OR `||` evaluates its left operand `alert(1)`. That shows the first message with `1`. -2. The `alert` returns `undefined`, so OR goes on to the second operand searching for a truthy value. -3. The second operand `2` is truthy, so the execution is halted, `2` is returned and then shown by the outer alert. +1. اولین OR `||` عملوند سمت چپ خود `alert(1)` را ارزیابی میکند. آن اولین پیام که `1` است را نمایش می دهد. +2. `alert` مقدار `undefined` را بر می گرداند، پس OR به سمت عملوند دوم برای پیدا کردن یک مقدار truthy ادامه می دهد. +3. عملوند دوم `2` turthy است، پس عملیات متوقف شده، `2` بر گردانده می شود و سپس توسط alert بیرونی نمایش داده می شود. -There will be no `3`, because the evaluation does not reach `alert(3)`. +هیچ `3`ای در کار نخواهد بود، چون ارزیابی به `alert(3)` نمی رسد. diff --git a/1-js/02-first-steps/11-logical-operators/2-alert-or/task.md b/1-js/02-first-steps/11-logical-operators/2-alert-or/task.md index 3908fa2ec..854e6ff1e 100644 --- a/1-js/02-first-steps/11-logical-operators/2-alert-or/task.md +++ b/1-js/02-first-steps/11-logical-operators/2-alert-or/task.md @@ -2,9 +2,9 @@ importance: 3 --- -# What's the result of OR'ed alerts? +# نتیجه alert های دارای OR چیست؟ -What will the code below output? +کد پایین چه چیزی را خروجی خواهد داد؟ ```js alert( alert(1) || 2 || alert(3) ); diff --git a/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/solution.md b/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/solution.md index 5c2455ef4..0b9ffaeda 100644 --- a/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/solution.md +++ b/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/solution.md @@ -1,4 +1,4 @@ -The answer: `null`, because it's the first falsy value from the list. +جواب: `null`، چون اولین مقدار falsy از لیست است. ```js run alert( 1 && null && 2 ); diff --git a/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/task.md b/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/task.md index 043d431e4..3103cfab8 100644 --- a/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/task.md +++ b/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/task.md @@ -2,9 +2,9 @@ importance: 5 --- -# What is the result of AND? +# نتیجه AND جیست؟ -What is this code going to show? +این کد چه چیزی را نمایش خواهد داد؟ ```js alert( 1 && null && 2 ); diff --git a/1-js/02-first-steps/11-logical-operators/4-alert-and/solution.md b/1-js/02-first-steps/11-logical-operators/4-alert-and/solution.md index b6fb10d72..39387d36f 100644 --- a/1-js/02-first-steps/11-logical-operators/4-alert-and/solution.md +++ b/1-js/02-first-steps/11-logical-operators/4-alert-and/solution.md @@ -1,10 +1,10 @@ -The answer: `1`, and then `undefined`. +جواب: `1`، و سپس `undefined`. ```js run alert( alert(1) && alert(2) ); ``` -The call to `alert` returns `undefined` (it just shows a message, so there's no meaningful return). +صدا زدن `alert` `undefined` را بر می گرداند (آن فقط یک پیام را نمایش می دهد، پس هیچ return معناداری وجود ندارد). -Because of that, `&&` evaluates the left operand (outputs `1`), and immediately stops, because `undefined` is a falsy value. And `&&` looks for a falsy value and returns it, so it's done. +به خاطر آن، `&&` عملوند چپ را ارزیابی میکند (`1` را نمایش می دهد)، و بلافاصله متوقف می شود، چون `undefined` یک مقدار falsy است. و `&&` به دنبال یک مقدار falsy می گردد و آن را بر می گرداند، بنابراین کار تمام می شود. diff --git a/1-js/02-first-steps/11-logical-operators/4-alert-and/task.md b/1-js/02-first-steps/11-logical-operators/4-alert-and/task.md index 69f877b95..b4654b88c 100644 --- a/1-js/02-first-steps/11-logical-operators/4-alert-and/task.md +++ b/1-js/02-first-steps/11-logical-operators/4-alert-and/task.md @@ -2,9 +2,9 @@ importance: 3 --- -# What is the result of AND'ed alerts? +# نتیجه alert های دارای AND چیست؟ -What will this code show? +این کد چه چیزی را نمایش خواهد داد؟ ```js alert( alert(1) && alert(2) ); diff --git a/1-js/02-first-steps/11-logical-operators/5-alert-and-or/solution.md b/1-js/02-first-steps/11-logical-operators/5-alert-and-or/solution.md index 25e3568f8..111727825 100644 --- a/1-js/02-first-steps/11-logical-operators/5-alert-and-or/solution.md +++ b/1-js/02-first-steps/11-logical-operators/5-alert-and-or/solution.md @@ -1,16 +1,16 @@ -The answer: `3`. +جواب: `3`. ```js run alert( null || 2 && 3 || 4 ); ``` -The precedence of AND `&&` is higher than `||`, so it executes first. +اولویت AND `&&` از `||` بیشتر است، ینابراین اول اجرا می شود. -The result of `2 && 3 = 3`, so the expression becomes: +نتیجه `3 = 3 && 2`، پس عبارت تبدیل می شود به: ``` null || 3 || 4 ``` -Now the result is the first truthy value: `3`. +حالا نتیجه اولین مقدار truthy است: `3`. diff --git a/1-js/02-first-steps/11-logical-operators/5-alert-and-or/task.md b/1-js/02-first-steps/11-logical-operators/5-alert-and-or/task.md index b18bb9c51..9bf36515d 100644 --- a/1-js/02-first-steps/11-logical-operators/5-alert-and-or/task.md +++ b/1-js/02-first-steps/11-logical-operators/5-alert-and-or/task.md @@ -2,9 +2,9 @@ importance: 5 --- -# The result of OR AND OR +# نتیجه OR AND OR -What will the result be? +نتیجه چه خواهد بود؟ ```js alert( null || 2 && 3 || 4 ); diff --git a/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md b/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md index fc9e336c1..4aa133662 100644 --- a/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md +++ b/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md @@ -2,8 +2,8 @@ importance: 3 --- -# Check the range between +# حدود بین را بررسی کنید -Write an `if` condition to check that `age` is between `14` and `90` inclusively. +یک شرط `if` بنویسید که بررسی کند `age` بین `14` و `90` به صورتی که شامل خودشان هم بشود هست یا نه. -"Inclusively" means that `age` can reach the edges `14` or `90`. +"شامل خودشان" یعنی `age` می تواند به مرز `14` و `90` هم برسد. diff --git a/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/solution.md b/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/solution.md index d1946a967..cc21c333d 100644 --- a/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/solution.md +++ b/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/solution.md @@ -1,10 +1,10 @@ -The first variant: +نوع اول: ```js if (!(age >= 14 && age <= 90)) ``` -The second variant: +نوع دوم: ```js if (age < 14 || age > 90) diff --git a/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/task.md b/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/task.md index 9b947d00f..1b7f6f518 100644 --- a/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/task.md +++ b/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/task.md @@ -2,8 +2,8 @@ importance: 3 --- -# Check the range outside +# محدوده خارج را بررسی کنید -Write an `if` condition to check that `age` is NOT between `14` and `90` inclusively. +یک شرط `if` بنویسید که بررسی کند `age` بین `14` و `90` به صورتی که که شامل خود آنها هم بشود نباشد. -Create two variants: the first one using NOT `!`, the second one -- without it. +دو نوع بسازید: اولی با استفاده از NOT `!`، و دومی بدون آن. diff --git a/1-js/02-first-steps/11-logical-operators/8-if-question/solution.md b/1-js/02-first-steps/11-logical-operators/8-if-question/solution.md index 210509758..005a18b06 100644 --- a/1-js/02-first-steps/11-logical-operators/8-if-question/solution.md +++ b/1-js/02-first-steps/11-logical-operators/8-if-question/solution.md @@ -1,19 +1,19 @@ -The answer: the first and the third will execute. +جواب: اولی و سومی اجرا خواهند شد. -Details: +جزییات: ```js run -// Runs. -// The result of -1 || 0 = -1, truthy +// اجرا می شود. +// نتیجه 1- = 0 || 1-، truthy if (-1 || 0) alert( 'first' ); -// Doesn't run +// اجرا نمی شود. // -1 && 0 = 0, falsy if (-1 && 0) alert( 'second' ); -// Executes -// Operator && has a higher precedence than || -// so -1 && 1 executes first, giving us the chain: +// اجرا می شود +// عملوند && اولویت بیشتری نسبت به || دارد +// پس 1 && 1- اول اجرا می شود، و به ما زنجیره را می دهد: // null || -1 && 1 -> null || 1 -> 1 if (null || -1 && 1) alert( 'third' ); ``` diff --git a/1-js/02-first-steps/11-logical-operators/8-if-question/task.md b/1-js/02-first-steps/11-logical-operators/8-if-question/task.md index 55987121b..1eedba0f0 100644 --- a/1-js/02-first-steps/11-logical-operators/8-if-question/task.md +++ b/1-js/02-first-steps/11-logical-operators/8-if-question/task.md @@ -2,11 +2,11 @@ importance: 5 --- -# A question about "if" +# یک سوال درباره "if" -Which of these `alert`s are going to execute? +کدام یک از `alert`ها اجرا خواهد شد؟ -What will the results of the expressions be inside `if(...)`? +نتیجه عبارت های داخل `if(...)` چه خواهد بود؟ ```js if (-1 || 0) alert( 'first' ); diff --git a/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md b/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md index 604606259..ecb8b1f6c 100644 --- a/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md +++ b/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md @@ -22,4 +22,4 @@ if (userName === 'Admin') { } ``` -Note the vertical indents inside the `if` blocks. They are technically not required, but make the code more readable. +به تورفتگی عمودی درون بلوک های `if` توجه کنید. از لحاظ فنی به آنها نیازی نیست، اما کد را خواناتر می کنند. diff --git a/1-js/02-first-steps/11-logical-operators/9-check-login/task.md b/1-js/02-first-steps/11-logical-operators/9-check-login/task.md index 290a52642..3e17692b0 100644 --- a/1-js/02-first-steps/11-logical-operators/9-check-login/task.md +++ b/1-js/02-first-steps/11-logical-operators/9-check-login/task.md @@ -2,24 +2,24 @@ importance: 3 --- -# Check the login +# login را بررسی کنید -Write the code which asks for a login with `prompt`. +کدی بنویسید که با `prompt` برای login درخواست کند. -If the visitor enters `"Admin"`, then `prompt` for a password, if the input is an empty line or `key:Esc` -- show "Canceled", if it's another string -- then show "I don't know you". +اگر بازدید کننده `"Admin"` وارد کند، سپس برای رمز عبور `prompt` کنید، اگر ورودی یک خط خالی یا `key:Esc` باشد -- "Canceled" را نمایش دهید، اگر رشته(string) دیگری باشد -- سپس "I don't know you" را نشان دهید. -The password is checked as follows: +رمز عبور به شکل زیر بررسی می شود: -- If it equals "TheMaster", then show "Welcome!", -- Another string -- show "Wrong password", -- For an empty string or cancelled input, show "Canceled" +- اگر برابر با "TheMaster" باشد، سپس "Welcome!" را نمایش دهید، +- اگر رشته(string) دیگری باشد -- "Wrong password" را نمایش دهید، +- اگر یک رشته(string) خالی یا ورودی cancelled باشد، "Canceled" را نمایش دهید -The schema: +طرح: ![](ifelse_task.svg) -Please use nested `if` blocks. Mind the overall readability of the code. +لطفا از بلوک های `if` تو در تو استفاده کنید. خوانایی کلی کد را در نظر بگیرید. -Hint: passing an empty input to a prompt returns an empty string `''`. Pressing `key:ESC` during a prompt returns `null`. +راهنمایی جزیی: رد کردن یک ورودی خالی به prompt یک رشته خالی `''` بر می گرداند. فشار دادن `key:ESC` در حین prompt `null` را بر می گرداند. -[demo] +[دمو] diff --git a/1-js/02-first-steps/11-logical-operators/article.md b/1-js/02-first-steps/11-logical-operators/article.md index 97f5d738a..2ab114a60 100644 --- a/1-js/02-first-steps/11-logical-operators/article.md +++ b/1-js/02-first-steps/11-logical-operators/article.md @@ -1,24 +1,24 @@ -# Logical operators +# عملگر های منطقی -There are four logical operators in JavaScript: `||` (OR), `&&` (AND), `!` (NOT), `??` (Nullish Coalescing). Here we cover the first three, the `??` operator is in the next article. +چهار عملگر منطقی در جاوااسکریپت وجود دارد: `||` (OR)، `&&` (AND)، `!` (NOT)، `??` (Nullish Coalescing). اینجا ما سه تای اول را پوشش می دهیم، عملگر `??` در مقاله بعدی است. -Although they are called "logical", they can be applied to values of any type, not only boolean. Their result can also be of any type. +با اینکه آنها "منطقی" نام برده می شوند، می توانند روی هر نوع مقداری اعمال شوند، نه فقط نوع boolean. نتیجه آنها هم می تواند از هر نوعی باشد. -Let's see the details. +بیایید جزییات را ببینیم. ## || (OR) -The "OR" operator is represented with two vertical line symbols: +عملگر "OR(یا)" با نماد دو خط عمود نمایش داده می شود: ```js result = a || b; ``` -In classical programming, the logical OR is meant to manipulate boolean values only. If any of its arguments are `true`, it returns `true`, otherwise it returns `false`. +در برنامه نویسی کلاسیک، عملگر منطقی OR تنها با مقدارهای boolean کار می کرد. اگر هر کدام از آرگومان های آن `true` باشد، `ture` بر می گرداند، در غیر این صورت `false` بر می گرداند. -In JavaScript, the operator is a little bit trickier and more powerful. But first, let's see what happens with boolean values. +در جاوااسکریپت، این عملگر مقداری فوت و فن و قدرت بیشتری دارد. اما اول، بیایید ببینیم با مقدارهای boolean چه اتفاقی می افتد. -There are four possible logical combinations: +چهار ترکیب احتمالی منطقی وجود دارد: ```js run alert( true || true ); // true @@ -27,21 +27,21 @@ alert( true || false ); // true alert( false || false ); // false ``` -As we can see, the result is always `true` except for the case when both operands are `false`. +همانطور که می بینیم، نتیجه همیشه `true` است به جز موقعی که هر دو عملوند `false` باشند. -If an operand is not a boolean, it's converted to a boolean for the evaluation. +اگر یک عملوند boolean نباشد، برای ارزیابی به boolean تبدیل می شود. -For instance, the number `1` is treated as `true`, the number `0` as `false`: +برای مثال، با عدد `1` مانند `true` رفتار می شود، با عدد `0` مانند `false`: ```js run -if (1 || 0) { // works just like if( true || false ) +if (1 || 0) { // همانند if( true || false ) کار میکند alert( 'truthy!' ); } ``` -Most of the time, OR `||` is used in an `if` statement to test if *any* of the given conditions is `true`. +اکثر اوقات، OR `||` به عنوان یک دستور `if` استفاده می شود تا بررسی شود که آیا *هر کدام* از شرط های داده شده `true` هست یا نه. -For example: +برای مثال: ```js run let hour = 9; @@ -49,61 +49,61 @@ let hour = 9; *!* if (hour < 10 || hour > 18) { */!* - alert( 'The office is closed.' ); + alert( 'اداره بسته است.' ); } ``` -We can pass more conditions: +ما می توانیم شرط های بیشتری قرار بدهیم: ```js run let hour = 12; let isWeekend = true; if (hour < 10 || hour > 18 || isWeekend) { - alert( 'The office is closed.' ); // it is the weekend + alert( 'اداره بسته است.' ); // آخر هفته است } ``` -## OR "||" finds the first truthy value [#or-finds-the-first-truthy-value] +## OR "||" اولین مقدار truthy را پیدا می کند [#or-finds-the-first-truthy-value] -The logic described above is somewhat classical. Now, let's bring in the "extra" features of JavaScript. +منطقی که بالا توصیف شد تا حدی کلاسیک است. الان، بیایید وارد ویژگی های "اضافه" جاوااسکریپت شویم. -The extended algorithm works as follows. +الگوریتم قابل تعمیم به صورت زیر عمل می کند. -Given multiple OR'ed values: +چند مقدار همراه با OR وارد می کنیم: ```js result = value1 || value2 || value3; ``` -The OR `||` operator does the following: +عملگر OR `||` مراحل پایین را انجام می دهد: -- Evaluates operands from left to right. -- For each operand, converts it to boolean. If the result is `true`, stops and returns the original value of that operand. -- If all operands have been evaluated (i.e. all were `false`), returns the last operand. +- عملوند ها را از چپ به راست ارزیابی می کند. +- هر عملوند را، تبدیل به boolean می کند. اگر نتیجه `true` باشد، متوقف می شود و مقدار اصلی عملوند را بر می گرداند. +- اگر تمام عملوند ها ارزیابی شدند (یعنی تمام آنها `false` بودند)، عملوند آخر را بر می گرداند. -A value is returned in its original form, without the conversion. +هر مقدار با شکل اصلی خود و بدون تبدیل بر گردانده می شود. -In other words, a chain of OR `||` returns the first truthy value or the last one if no truthy value is found. +به عبارتی دیگر، یک زنجیره از OR `||` اولین مقدار truthy یا در صورتی که هیج مقدار truthy پیدا نشود آخرین مقدار را بر می گرداند. -For instance: +برای مثال: ```js run -alert( 1 || 0 ); // 1 (1 is truthy) +alert( 1 || 0 ); // 1 (1 truthy است) -alert( null || 1 ); // 1 (1 is the first truthy value) -alert( null || 0 || 1 ); // 1 (the first truthy value) +alert( null || 1 ); // 1 (1 اولین مقدار truthy است) +alert( null || 0 || 1 ); // 1 (اولین مقدار truthy) -alert( undefined || null || 0 ); // 0 (all falsy, returns the last value) +alert( undefined || null || 0 ); // 0 (همه falsy هستند، آخرین مقدار را بر می گرداند) ``` -This leads to some interesting usage compared to a "pure, classical, boolean-only OR". +این باعث چند استفاده جالب نسبت به "OR خالص، کلاسیک، فقط-boolean" می شود. -1. **Getting the first truthy value from a list of variables or expressions.** +1. **گرفتن اولین مقدار truthy از یک لیست متغیرها یا عبارت ها.** - For instance, we have `firstName`, `lastName` and `nickName` variables, all optional (i.e. can be undefined or have falsy values). + برای مثال، ما متغیر های `firstName`، `lastName` و `nickName` داریم، همه آنها اختیاری هستند (یعنی می توانند undefined یا مقدارهای falsy داشته باشند). - Let's use OR `||` to choose the one that has the data and show it (or `"Anonymous"` if nothing set): + بیایید با استفاده از OR `||` متغیری که دارای داده است را انتخاب کنیم و آن را نمایش دهیم (یا اگر چیزی تنظیم نشده باشد `Anonymous` را): ```js run let firstName = ""; @@ -115,36 +115,36 @@ This leads to some interesting usage compared to a "pure, classical, boolean-onl */!* ``` - If all variables were falsy, `"Anonymous"` would show up. + اگر تمام متغیرها falsy بودند، `"Anonymous"` نمایش داده می شد. -2. **Short-circuit evaluation.** +2. **ارزیابی گردش کوتاه.** - Another feature of OR `||` operator is the so-called "short-circuit" evaluation. + یکی دیگر از ویژگی های اپراتور OR `||` به اصطلاح ارزیابی "گردش کوتاه" است. - It means that `||` processes its arguments until the first truthy value is reached, and then the value is returned immediately, without even touching the other argument. + یعنی اینکه `||` روی آرگومان های خودش پردازش انجام می دهد تا زمانی که به اولین مقدار truthy برسد، و سپس آن مقدار بلافاصله بر گردانده می شود، بدون اینکه به بقیه آرگومان ها کاری داشته باشد. - That importance of this feature becomes obvious if an operand isn't just a value, but an expression with a side effect, such as a variable assignment or a function call. + اهمیت این ویژگی زمانی آشکار می شود که یک عملوند فقط یک مقدار ساده نباشد، بلکه یک عبارت با یک اثر جانبی باشد، مثل تخصیص متغیر یا صدا زدن تابع. - In the example below, only the second message is printed: + در مثال پایین، فقط پیام دوم چاپ می شود: ```js run no-beautify - *!*true*/!* || alert("not printed"); - *!*false*/!* || alert("printed"); + *!*true*/!* || alert("چاپ نمی شود"); + *!*false*/!* || alert("چاپ می شود"); ``` - In the first line, the OR `||` operator stops the evaluation immediately upon seeing `true`, so the `alert` isn't run. + در خط اول، عملگر OR `||` به محض دیدن `true` ارزیابی را بلافاصله متوقف می کند، پس `alert` اجرا نمی شود. - Sometimes, people use this feature to execute commands only if the condition on the left part is falsy. + بعضی اوقات، افراد از این ویژگی استفاده می کنند تا دستوراتی را فقط اگر شرط سمت چپ falsy باشد اجرا کنند. ## && (AND) -The AND operator is represented with two ampersands `&&`: +عملگر AND(و) با دو ampersand `&&` نمایش داده می شود: ```js result = a && b; ``` -In classical programming, AND returns `true` if both operands are truthy and `false` otherwise: +در برنامه نویسی کلاسیک، AND اگر هر دو عملوند truthy باشند `true` را بر می گرداند و در غیر این صورت `false`: ```js run alert( true && true ); // true @@ -153,137 +153,136 @@ alert( true && false ); // false alert( false && false ); // false ``` -An example with `if`: +یک مثال با `if`: ```js run let hour = 12; let minute = 30; if (hour == 12 && minute == 30) { - alert( 'The time is 12:30' ); + alert( 'ساعت 12:30 است' ); } ``` -Just as with OR, any value is allowed as an operand of AND: +درست مثل OR، هر مقداری به عنوان عملوند AND مجاز است: ```js run -if (1 && 0) { // evaluated as true && false - alert( "won't work, because the result is falsy" ); +if (1 && 0) { // به عنوان true && false ارزیابی می شود + alert( "کار نخواهد کرد، چون نتیجه falsy است" ); } ``` +## AND "&&" اولین مقدار falsy را پیدا می کند -## AND "&&" finds the first falsy value - -Given multiple AND'ed values: +چند مقدار را همراه AND می دهیم: ```js result = value1 && value2 && value3; ``` -The AND `&&` operator does the following: +عملگر AND `&&` مراحل زیر را انجام می دهد: -- Evaluates operands from left to right. -- For each operand, converts it to a boolean. If the result is `false`, stops and returns the original value of that operand. -- If all operands have been evaluated (i.e. all were truthy), returns the last operand. +- عملوند ها را از چپ به راست ارزیابی می کند. +- هر عملوند را به boolean تبدیل می کند. اگر نتیجه `false` باشد، متوقف می شود و مقدار اصلی آن عملوند را بر می گرداند. +- اگر تمام عملوند ها ارزیابی شدند (یعنی همه truthy بودند)، آخرین عملوند را بر می گرداند. -In other words, AND returns the first falsy value or the last value if none were found. +به عبارتی دیگر، AND اولین مقدار falsy یا اگر هیچ falsy پیدا نشد آخرین مقدار را بر می گرداند. -The rules above are similar to OR. The difference is that AND returns the first *falsy* value while OR returns the first *truthy* one. +قوانین بالا شبیه به قوانین OR هستند. تقاوت این است که AND اولین مقدار *falsy* را بر می گرداند در حالی که OR اولین مقدار *truthy* را بر می گرداند. -Examples: +مثال ها: ```js run -// if the first operand is truthy, -// AND returns the second operand: +// اگر عملوند اول truthy باشد، +// AND عملوند دوم را بر می گرداند: alert( 1 && 0 ); // 0 alert( 1 && 5 ); // 5 -// if the first operand is falsy, -// AND returns it. The second operand is ignored +// اگر عملوند اول falsy باشد، +// AND آن را بر می گرداند. عملوند دوم نادیده گرفته می شود. alert( null && 5 ); // null -alert( 0 && "no matter what" ); // 0 +alert( 0 && "فرقی ندارد که چه چیزی باشد" ); // 0 ``` -We can also pass several values in a row. See how the first falsy one is returned: +ما همچنین می توانیم چند مقدار را در یک ردیف قرار بدهیم. ببینید چگونه اولین مقدار falsy بر گردانده می شود. ```js run alert( 1 && 2 && null && 3 ); // null ``` -When all values are truthy, the last value is returned: +وقتی که تمام مقدارها truthy باشند، آخرین مقدار بر گردانده می شود: ```js run -alert( 1 && 2 && 3 ); // 3, the last one +alert( 1 && 2 && 3 ); // 3, آخرین مقدار ``` -````smart header="Precedence of AND `&&` is higher than OR `||`" -The precedence of AND `&&` operator is higher than OR `||`. +````smart header="اولویت AND `&&` از OR `||` بیشتر است" +اولویت عملگر AND `&&` بالاتر از OR `||` است. -So the code `a && b || c && d` is essentially the same as if the `&&` expressions were in parentheses: `(a && b) || (c && d)`. +بنابراین کد `a && b || c && d` اساسا مانند این است که عبارت `&&` داخل پرانتر باشد: `(a && b) || (c && d)`. ```` -````warn header="Don't replace `if` with `||` or `&&`" -Sometimes, people use the AND `&&` operator as a "shorter way to write `if`". +````warn header="`if` را با `||` یا `&&` جابجا نکنید" +بعضی اوقات، افراد از عملگر AND `&&` به عنوان "راه کوتاه تر برای نوشتن `if`" استفاده می کنند. -For instance: +برای مثال: ```js run let x = 1; -(x > 0) && alert( 'Greater than zero!' ); +(x > 0) && alert( 'بزرگ تر از صفر!' ); ``` -The action in the right part of `&&` would execute only if the evaluation reaches it. That is, only if `(x > 0)` is true. +عمل سمت راست `&&` فقط اگر ارزیابی به آن برسد اجرا می شود. یعنی اینکه، فقط اگر `(x > 0)` درست باشد. -So we basically have an analogue for: +پس ما اساسا یک تشبیه برای کد زیر داریم: ```js run let x = 1; -if (x > 0) alert( 'Greater than zero!' ); +if (x > 0) alert( 'بزرگ تر از صفر!' ); ``` -Although, the variant with `&&` appears shorter, `if` is more obvious and tends to be a little bit more readable. So we recommend using every construct for its purpose: use `if` if we want `if` and use `&&` if we want AND. +با اینکه نوعی که همراه با `&&` است ظاهر کوتاه تری دارد، `if` واضح تر است و کمی خواناتر است. پس ما پیشنهاد ما این است که هر ساختار را برای هدف خودش استفاده کنیم: از `if` در صورتی که `if` می خواهیم استفاده کنیم و از `&&` در صورتی که AND می خواهیم استفاده کنیم. ```` ## ! (NOT) -The boolean NOT operator is represented with an exclamation sign `!`. +عملگر NOT(نفی) با یک علامت تعجب `!` نمایش داده می شود. -The syntax is pretty simple: +سینتکس بسیار ساده است: ```js result = !value; ``` -The operator accepts a single argument and does the following: +عملگر یک آرگومان قبول می کند و مراحل زیر را انجام می دهد: -1. Converts the operand to boolean type: `true/false`. -2. Returns the inverse value. +1. عملوند را به نوع boolean تبدیل می کند: `true/false`. +2. مقدار معکوس را بر می گرداند. -For instance: +برای مثال: ```js run alert( !true ); // false alert( !0 ); // true ``` -A double NOT `!!` is sometimes used for converting a value to boolean type: +بعضی اوقات NOT دوگانه `!!` برای تبدیل یک مقدار به نوع boolean استفاده می شود: ```js run -alert( !!"non-empty string" ); // true +alert( !!"string که خالی نیست" ); // true alert( !!null ); // false ``` -That is, the first NOT converts the value to boolean and returns the inverse, and the second NOT inverses it again. In the end, we have a plain value-to-boolean conversion. +یعنی اینکه، اولین NOT مقدار را به boolean تبدیل می کند و معکوس آن را بر می گرداند، و دومین NOT دوباره آن را معکوس می کند. سرانجام، ما یک تبدیل ساده مقدار به boolean خواهیم داشت. -There's a little more verbose way to do the same thing -- a built-in `Boolean` function: +یک راه کلامی تر برای انجام کار مشابه وجود دارد -- تابع درون ساخت `Boolean`: ```js run -alert( Boolean("non-empty string") ); // true +alert( Boolean("string که خالی نیست") ); // true alert( Boolean(null) ); // false ``` -The precedence of NOT `!` is the highest of all logical operators, so it always executes first, before `&&` or `||`. +عملگر NOT `!` بالاترین اولویت را بین عملگر های منطقی دارد، بنابراین همیشه اول اجرا می شود، قبل از `&&` یا `||`.