Skip to content

Commit b8ce237

Browse files
committed
translate part 1, 2.12- nullish coalescring operator to AR
1 parent f3df8c6 commit b8ce237

1 file changed

Lines changed: 38 additions & 38 deletions

File tree

  • 1-js/02-first-steps/12-nullish-coalescing-operator
Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
# Nullish coalescing operator '??'
1+
# Nullish Coalescing Operator '??'
22

33
[recent browser="new"]
44

5-
The nullish coalescing operator `??` provides a short syntax for selecting a first "defined" variable from the list.
5+
Nullish Coalescing Operator `??` يقدم طريقة مختصرة لإختيار أول قيمة معرفة من قائمة متغيرات.
66

7-
The result of `a ?? b` is:
8-
- `a` if it's not `null` or `undefined`,
9-
- `b`, otherwise.
7+
نتيجة `a ?? b` تكون:
8+
- `a` إذا لم تكن `null` أو `undefined`,
9+
- `b`, غير ذلك.
1010

11-
So, `x = a ?? b` is a short equivalent to:
11+
لذلك فإن `x = a ?? b` طريقة مختصرة للتالي:
1212

1313
```js
1414
x = (a !== null && a !== undefined) ? a : b;
1515
```
1616

17-
Here's a longer example.
17+
هذا مثال أطول.
1818

19-
Let's say, we have a `firstName`, `lastName` or `nickName`, all of them optional.
19+
لنفترض أن لدينا `firstName`, `lastName` أو `nickName` وجميعهم اختياريين.
2020

21-
Let's choose the defined one and show it (or "Anonymous" if nothing is set):
21+
لنختار القيمة المعرفة ونعرضها (أو نعرض "Anonymous" إذا لم يحدد أي شئ):
2222

2323
```js run
2424
let firstName = null;
@@ -29,25 +29,25 @@ let nickName = "Supercoder";
2929
alert(firstName ?? lastName ?? nickName ?? "Anonymous"); // Supercoder
3030
```
3131
32-
## Comparison with ||
32+
## المقارنة مع ||
3333
34-
That's very similar to OR `||` operator. Actually, we can replace `??` with `||` in the code above and get the same result.
34+
هذا مشابه جدًا للمعامل `||`. في الحقيقة يمكننا استبدال `??` ب `||` في المثال السابق وسنحصل على نفس النتيجة.
3535
36-
The important difference is that:
37-
- `||` returns the first *truthy* value.
38-
- `??` returns the first *defined* value.
36+
الفرق الجوهري بينهما أن:
37+
- `||` يرجع أول قيمة *truthy*.
38+
- `??` يرجع أول قيمة *defined*.
3939
40-
This matters a lot when we'd like to treat `null/undefined` differently from `0`.
40+
هذا مهم جدًا عندما نريد معاملة `null/undefined` بطريقة مختلفة عن `0`.
4141
42-
For example:
42+
مثلًا:
4343
4444
```js
4545
height = height ?? 100;
4646
```
4747
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` سيبقى كما هو.
4949
50-
Let's compare it with `||`:
50+
لنقارنه مع `||`:
5151
5252
```js run
5353
let height = 0;
@@ -56,62 +56,62 @@ alert(height || 100); // 100
5656
alert(height ?? 100); // 0
5757
```
5858
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 أخرىوهذا قد لا يكون صحيح أحيانًا.
6060
61-
The `height ?? 100` returns `100` only if `height` is exactly `null` or `undefined`.
61+
ولكن `height ?? 100` ترجع `100` إذا كان فقط `height` يساوي تمامًا `null` أو `undefined`.
6262
63-
## Precedence
63+
## الأولوية
6464
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).
65+
أولوية المعامل `??` هي قليلة: `7` وتساوي [MDN جدول](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table).
6666
67-
That's lower than most operators and a bit higher than `=` and `?`.
67+
هذا أقل من معظم المعاملات وأكبر بقليل من `=` و `?`.
6868
69-
So if we need to use `??` in a complex expression, then consider adding parentheses:
69+
لذلك إذا أردنا استخدام `??` في تعبيرات معقدة نقوم بإضافة أقواس:
7070
7171
```js run
7272
let height = null;
7373
let width = null;
7474

75-
// important: use parentheses
75+
// مهم: استخدم الأقواس
7676
let area = (height ?? 100) * (width ?? 50);
7777

7878
alert(area); // 5000
7979
```
8080
81-
Otherwise, if we omit parentheses, then `*` has the higher precedence and would run first. That would be the same as:
81+
إذا لم نستخدم الأقواس فإن `*` له أولوية أعلى وسينفذ أولًا كأننا كتبنا:
8282
8383
```js
84-
// not correct
84+
// غير صحيح
8585
let area = height ?? (100 * width) ?? 50;
8686
```
8787
88-
There's also a related language-level limitation. Due to safety reasons, it's forbidden to use `??` together with `&&` and `||` operators.
88+
هناك أيضًا قيود لغوية. لأسباب أمنية لا يمكن استخدام `??` مع `&&` أو `||`.
8989
90-
The code below triggers a syntax error:
90+
هذا سينتج خطأ لغوي:
9191
9292
```js run
9393
let x = 1 && 2 ?? 3; // Syntax error
9494
```
9595
96-
The limitation is surely debatable, but for some reason it was added to the language specification.
96+
هذا القيد قد لا يبدو منطقيًا ولكن لبعض الأسباب تم إضافته للغة.
9797
98-
Use explicit parentheses to fix it:
98+
استخدم الأقواس لتجنب الخطأ:
9999
100100
```js run
101-
let x = (1 && 2) ?? 3; // Works
101+
let x = (1 && 2) ?? 3; // تعمل دون مشاكل
102102
alert(x); // 2
103103
```
104104
105-
## Summary
105+
## ملخص
106106
107-
- The nullish coalescing operator `??` provides a short way to choose a "defined" value from the list.
107+
- معامل حذف null `??` يقدم طريقة مختصرة لإختيار أول قيمة معرفة من قائمة قيم.
108108
109-
It's used to assign default values to variables:
109+
يستخدم لوضع قيم افتراضية للمتغيرات:
110110
111111
```js
112-
// set height=100, if height is null or undefined
112+
// اجعل height=100 إذا كان null أو undefined
113113
height = height ?? 100;
114114
```
115115
116-
- The operator `??` has a very low precedence, a bit higher than `?` and `=`.
117-
- It's forbidden to use it with `||` or `&&` without explicit parentheses.
116+
- المعامل `??` لديه أولوية قليلة جدًا لكن أعلة قليلًا من `?` و `=`.
117+
- يمنع استخدامه مع `||` أو `&&` بدون أقواس.

0 commit comments

Comments
 (0)