You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
That's very similar to OR `||` operator. Actually, we can replace `??` with `||` in the code above and get the same result.
هذا مشابه جدًا للمعامل `||`. في الحقيقة يمكننا استبدال `??` ب `||` في المثال السابق وسنحصل على نفس النتيجة.
The important difference is that:
- `||` returns the first *truthy* value.
- `??` returns the first *defined* value.
الفرق الجوهري بينهما أن:
- `||` يرجع أول قيمة *truthy*.
- `??` يرجع أول قيمة *defined*.
This matters a lot when we'd like to treat `null/undefined` differently from `0`.
هذا مهم جدًا عندما نريد معاملة `null/undefined` بطريقة مختلفة عن `0`.
For example:
مثلًا:
```js
height = height ?? 100;
```
This sets `height` to `100` if it's not defined. But if `height` is `0`, then it remains "as is".
هذا يجعل `height` يساوي `100` إذا لم يعرف. ولكن إذا كان `height` يساوي `0` سيبقى كما هو.
Let's compare it with `||`:
لنقارنه مع `||`:
```js run
let height = 0;
Expand All
@@ -56,62 +56,62 @@ alert(height || 100); // 100
alert(height ?? 100); // 0
```
Here, `height || 100` treats zero height as unset, same as `null`, `undefined` or any other falsy value, depeding on use cases that may be incorrect.
هنا `height || 100` تعامل الصفر مثل `null`, `undefined` أو أي قيمة falsy أخرىوهذا قد لا يكون صحيح أحيانًا.
The `height ?? 100` returns `100` only if `height` is exactly `null` or `undefined`.
ولكن `height ?? 100` ترجع `100` إذا كان فقط `height` يساوي تمامًا `null` أو `undefined`.
## Precedence
## الأولوية
The precedence of the `??` operator is rather low: `7` in the [MDN table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table).
أولوية المعامل `??` هي قليلة: `7` وتساوي [MDN جدول](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table).
That's lower than most operators and a bit higher than `=` and `?`.
هذا أقل من معظم المعاملات وأكبر بقليل من `=` و `?`.
So if we need to use `??` in a complex expression, then consider adding parentheses:
لذلك إذا أردنا استخدام `??` في تعبيرات معقدة نقوم بإضافة أقواس:
```js run
let height = null;
let width = null;
// important: use parentheses
// مهم: استخدم الأقواس
let area = (height ?? 100) * (width ?? 50);
alert(area); // 5000
```
Otherwise, if we omit parentheses, then `*` has the higher precedence and would run first. That would be the same as:
إذا لم نستخدم الأقواس فإن `*` له أولوية أعلى وسينفذ أولًا كأننا كتبنا:
```js
// not correct
// غير صحيح
let area = height ?? (100 * width) ?? 50;
```
There's also a related language-level limitation. Due to safety reasons, it's forbidden to use `??` together with `&&` and `||` operators.
هناك أيضًا قيود لغوية. لأسباب أمنية لا يمكن استخدام `??` مع `&&` أو `||`.
The code below triggers a syntax error:
هذا سينتج خطأ لغوي:
```js run
let x = 1 && 2 ?? 3; // Syntax error
```
The limitation is surely debatable, but for some reason it was added to the language specification.
هذا القيد قد لا يبدو منطقيًا ولكن لبعض الأسباب تم إضافته للغة.
Use explicit parentheses to fix it:
استخدم الأقواس لتجنب الخطأ:
```js run
let x = (1 && 2) ?? 3; // Works
let x = (1 && 2) ?? 3; // تعمل دون مشاكل
alert(x); // 2
```
## Summary
## ملخص
- The nullish coalescing operator `??` provides a short way to choose a "defined" value from the list.
- معامل حذف null `??` يقدم طريقة مختصرة لإختيار أول قيمة معرفة من قائمة قيم.
It's used to assign default values to variables:
يستخدم لوضع قيم افتراضية للمتغيرات:
```js
// set height=100, if height is null or undefined
// اجعل height=100 إذا كان null أو undefined
height = height ?? 100;
```
- The operator `??` has a very low precedence, a bit higher than `?` and `=`.
- It's forbidden to use it with `||` or `&&` without explicit parentheses.
- المعامل `??` لديه أولوية قليلة جدًا لكن أعلة قليلًا من `?` و `=`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The task demonstrates how postfix/prefix forms can lead to different results when used in comparisons.
السؤال يوضح كيف يمكن للصيغ postfix/prefix أن تؤدي إلى نتائج مختلفة عندما تستخدم للمقارنة.
1. **From 1 to 4**
1. **من 1 إلى 4**
```js run
let i = 0;
while (++i < 5) alert( i );
```
The first value is `i = 1`, because `++i` first increments `i` and then returns the new value. So the first comparison is `1 < 5` and the `alert` shows `1`.
أول قيمة هي `i = 1` لأن `++i` أولًا تزيد `i` ثم تقوم بإرجاع القيمة الجديدة. لذلك فإن أول عملية مقارنة هي `1 < 5` ويقوم `alert` بعرض `1`.
Then follow `2, 3, 4…` -- the values show up one after another. The comparison always uses the incremented value, because `++` is before the variable.
ثم يتبع ب `2, 3, 4…` -- واحدًا بعد الآخر. المقارنة دائما تستخدم القيمة بعد الزيادة لأن `++` قبل المتغير.
Finally, `i = 4` is incremented to `5`, the comparison `while(5 < 5)` fails, and the loop stops. So `5` is not shown.
2. **From 1 to 5**
أخيرًا `i = 4`تزيد إلى `5` والمقارنة `while(5 < 5)` تفشل وتتوقف الحلقة. لذلك لا يتم عرض `5` .
2. **من 1 إلى 5**
```js run
let i = 0;
while (i++ < 5) alert( i );
```
The first value is again `i = 1`. The postfix form of `i++` increments `i` and then returns the *old* value, so the comparison `i++ < 5` will use `i = 0` (contrary to `++i < 5`).
أول قيمة هي `i = 1`. صيغة postfix `i++` تزيد `i` وترجع القيمة القديمة ولذلك تكون المقارنة `i++ < 5` تستخدم `i = 0` (على العكس من `++i < 5`).
But the `alert` call is separate. It's another statement which executes after the increment and the comparison. So it gets the current `i = 1`.
ولكن استدعاء `alert` منفصل. فهو ينفذ بعد الزيادة والمقارنة. لذلك يحصل على القيمة الحالية `i = 1`.
Then follow `2, 3, 4…`
ثم يتبعه `2, 3, 4…`
Let's stop on `i = 4`. The prefix form `++i` would increment it and use `5` in the comparison. But here we have the postfix form `i++`. So it increments `i` to `5`, but returns the old value. Hence the comparison is actually `while(4 < 5)` -- true, and the control goes on to `alert`.
لنتوقف عند `i = 4`. صيغة prefix `++i` تزيدها وتستخدم `5` في المقارنة. لكن postfix `i++` تزيد `i` إلى `5`وترجع القيمة القديمة. فتكون المقارنة `while(4 < 5)` -- true وينفذ `alert`.
The value `i = 5` is the last one, because on the next step `while(5 < 5)` is false.
قيمة `i = 5` هي آخر قيمة لأن في الخطوة التالية `while(5 < 5)` تكون false.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
translate part 1 2.12, 2.13 #16
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
Uh oh!
There was an error while loading. Please reload this page.
translate part 1 2.12, 2.13 #16
Changes from all commits
b8ce237a42c099File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing