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
That's very similar to OR `||` operator. Actually, we can replace`??`with`||`in the code above and get the same result.
34
+
هذا مشابه جدًا للمعامل `||`. في الحقيقة يمكننا استبدال`??`ب`||`في المثال السابق وسنحصل على نفس النتيجة.
35
35
36
-
The important difference is that:
37
-
- `||`returns the first *truthy* value.
38
-
- `??`returns the first *defined* value.
36
+
الفرق الجوهري بينهما أن:
37
+
- `||`يرجع أول قيمة *truthy*.
38
+
- `??`يرجع أول قيمة *defined*.
39
39
40
-
This matters a lot when we'd like to treat `null/undefined`differently from`0`.
40
+
هذا مهم جدًا عندما نريد معاملة `null/undefined`بطريقة مختلفة عن`0`.
41
41
42
-
For example:
42
+
مثلًا:
43
43
44
44
```js
45
45
height = height ??100;
46
46
```
47
47
48
-
This sets`height`to`100`if it's not defined. But if `height`is`0`, then it remains "as is".
48
+
هذا يجعل`height`يساوي`100`إذا لم يعرف. ولكن إذا كان `height`يساوي`0` سيبقى كما هو.
49
49
50
-
Let's compare it with`||`:
50
+
لنقارنه مع`||`:
51
51
52
52
```js run
53
53
let height =0;
@@ -56,62 +56,62 @@ alert(height || 100); // 100
56
56
alert(height ??100); // 0
57
57
```
58
58
59
-
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.
59
+
هنا`height ||100`تعامل الصفر مثل `null`, `undefined`أو أي قيمة falsy أخرىوهذا قد لا يكون صحيح أحيانًا.
60
60
61
-
The`height ??100`returns`100`only if `height`is exactly`null`or`undefined`.
61
+
ولكن`height ??100`ترجع`100`إذا كان فقط `height`يساوي تمامًا`null`أو`undefined`.
62
62
63
-
## Precedence
63
+
## الأولوية
64
64
65
-
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).
The task demonstrates how postfix/prefix forms can lead to different results when used in comparisons.
1
+
السؤال يوضح كيف يمكن للصيغ postfix/prefix أن تؤدي إلى نتائج مختلفة عندما تستخدم للمقارنة.
2
2
3
-
1.**From 1 to 4**
3
+
1.**من 1 إلى 4**
4
4
5
5
```js run
6
6
let i =0;
7
7
while (++i <5) alert( i );
8
8
```
9
9
10
-
The first value is `i = 1`, because`++i`first increments`i`and then returns the newvalue. So the first comparison is `1 < 5`and the `alert`shows`1`.
10
+
أول قيمة هي `i = 1` لأن`++i`أولًا تزيد`i`ثم تقوم بإرجاع القيمة الجديدة. لذلك فإن أول عملية مقارنة هي `1 < 5`ويقوم `alert`بعرض`1`.
11
11
12
-
Then follow `2, 3, 4…`--the values show up one after another. The comparison always uses the incremented value, because `++`is before the variable.
12
+
ثم يتبع ب `2, 3, 4…`--واحدًا بعد الآخر. المقارنة دائما تستخدم القيمة بعد الزيادة لأن `++`قبل المتغير.
13
13
14
-
Finally,`i = 4` is incremented to `5`, the comparison `while(5 < 5)`fails, and the loop stops. So`5`is not shown.
15
-
2.**From1to5**
14
+
أخيرًا`i = 4`تزيد إلى `5` والمقارنة `while(5 < 5)`تفشل وتتوقف الحلقة. لذلك لا يتم عرض `5` .
15
+
2.**من1إلى5**
16
16
17
17
```js run
18
18
let i = 0;
19
19
while (i++ < 5) alert( i );
20
20
```
21
21
22
-
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`).
22
+
أول قيمة هي `i = 1`. صيغة postfix `i++`تزيد`i`وترجع القيمة القديمة ولذلك تكون المقارنة `i++ < 5`تستخدم `i = 0` (على العكس من`++i < 5`).
23
23
24
-
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`.
24
+
ولكن استدعاء`alert`منفصل. فهو ينفذ بعد الزيادة والمقارنة. لذلك يحصل على القيمة الحالية`i = 1`.
25
25
26
-
Then follow `2, 3, 4…`
26
+
ثم يتبعه`2, 3, 4…`
27
27
28
-
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`.
28
+
لنتوقف عند `i = 4`. صيغة prefix `++i`تزيدها وتستخدم `5`في المقارنة. لكن postfix `i++` تزيد `i`إلى`5`وترجع القيمة القديمة. فتكون المقارنة `while(4 < 5)`--true وينفذ`alert`.
29
29
30
-
The value `i = 5`is the last one, because on the next step `while(5 < 5)`isfalse.
30
+
قيمة `i = 5`هي آخر قيمة لأن في الخطوة التالية `while(5 < 5)`تكونfalse.
0 commit comments