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
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/03-strict-mode/article.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ For example:
19
19
...
20
20
```
21
21
22
-
Quite soon we're going to learn functions (a way to group commands) soon, so let's note in advance that `"use strict"` can be put at the beginning of a function. Doing that enables strict mode in that function only. But usually people use it for the whole script.
22
+
Quite soon we're going to learn functions (a way to group commands), so let's note in advance that `"use strict"` can be put at the beginning of a function. Doing that enables strict mode in that function only. But usually people use it for the whole script.
23
23
24
24
````warn header="Ensure that \"use strict\" is at the top"
25
25
Please make sure that `"use strict"` is at the top of your scripts, otherwise strict mode may not be enabled.
As `BigInt` numbers are rarely needed, we don't cover them here, but devoted them a separate chapter <info:bigint>. Read it when you need such big numbers.
83
83
84
84
```smart header="Compatability issues"
85
-
Right now `BigInt` is supported in Firefox and Chrome, but not in Safari/IE/Edge.
85
+
Right now `BigInt` is supported in Firefox/Chrome/Edge, but not in Safari/IE.
86
86
```
87
87
88
88
## String
@@ -257,7 +257,7 @@ There are 8 basic data types in JavaScript.
257
257
258
258
-`number` for numbers of any kind: integer or floating-point, integers are limited by ±2<sup>53</sup>.
259
259
-`bigint` is for integer numbers of arbitrary length.
260
-
-`string` for strings. A string may have one or more characters, there's no separate single-character type.
260
+
-`string` for strings. A string may have zero or more characters, there's no separate single-character type.
261
261
-`boolean` for `true`/`false`.
262
262
-`null` for unknown values -- a standalone type that has a single value `null`.
263
263
-`undefined` for unassigned values -- a standalone type that has a single value `undefined`.
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/12-nullish-coalescing-operator/article.md
+64-1Lines changed: 64 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,36 +16,58 @@ x = (a !== null && a !== undefined) ? a : b;
16
16
17
17
هذا مثال أطول.
18
18
19
+
<<<<<<< HEAD
19
20
لنفترض أن لدينا `firstName`, `lastName` أو `nickName` وجميعهم اختياريين.
20
21
21
22
لنختار القيمة المعرفة ونعرضها (أو نعرض "Anonymous" إذا لم يحدد أي شئ):
23
+
=======
24
+
Imagine, we have a user, and there are variables `firstName`, `lastName` or `nickName` for their first name, last name and the nick name. All of them may be undefined, if the user decided not to enter any value.
25
+
26
+
We'd like to display the user name: one of these three variables, or show "Anonymous" if nothing is set.
27
+
28
+
Let's use the `??` operator to select the first defined one:
هذا مشابه جدًا للمعامل `||`. في الحقيقة يمكننا استبدال `??` ب `||` في المثال السابق وسنحصل على نفس النتيجة.
46
+
=======
47
+
The OR `||` operator can be used in the same way as `??`. Actually, we can replace `??` with `||` in the code above and get the same result, as it was described in the [previous chapter](info:logical-operators#or-finds-the-first-truthy-value).
48
+
>>>>>>> 69e44506c3e9dac74c282be37b55ba7ff122ae74
35
49
36
50
الفرق الجوهري بينهما أن:
37
51
- `||` يرجع أول قيمة *truthy*.
38
52
- `??` يرجع أول قيمة *defined*.
39
53
40
54
هذا مهم جدًا عندما نريد معاملة `null/undefined` بطريقة مختلفة عن `0`.
41
55
56
+
<<<<<<< HEAD
42
57
مثلًا:
58
+
=======
59
+
For example, consider this:
60
+
>>>>>>> 69e44506c3e9dac74c282be37b55ba7ff122ae74
43
61
44
62
```js
45
63
height = height ??100;
46
64
```
47
65
66
+
<<<<<<< HEAD
48
67
هذا يجعل `height` يساوي `100` إذا لم يعرف. ولكن إذا كان `height` يساوي `0` سيبقى كما هو.
68
+
=======
69
+
This sets `height` to `100` if it's not defined.
70
+
>>>>>>> 69e44506c3e9dac74c282be37b55ba7ff122ae74
49
71
50
72
لنقارنه مع `||`:
51
73
@@ -56,17 +78,31 @@ alert(height || 100); // 100
56
78
alert(height ??100); // 0
57
79
```
58
80
81
+
<<<<<<< HEAD
59
82
هنا `height ||100` تعامل الصفر مثل `null`, `undefined` أو أي قيمة falsy أخرىوهذا قد لا يكون صحيح أحيانًا.
60
83
61
84
ولكن `height ??100` ترجع `100` إذا كان فقط `height` يساوي تمامًا `null` أو `undefined`.
85
+
=======
86
+
Here, `height ||100` treats zero height as unset, same as `null`, `undefined` or any other falsy value. So zero becames `100`.
87
+
88
+
The `height ??100` returns `100` only if `height` is exactly `null` or `undefined`. So zero remains "as is".
89
+
90
+
Which behavior is better depends on a particular use case. When zero height is a valid value, that we shouldn't touch, then `??` is preferrable.
91
+
>>>>>>> 69e44506c3e9dac74c282be37b55ba7ff122ae74
62
92
63
93
## الأولوية
64
94
65
95
أولوية المعامل `??` هي قليلة: `7` وتساوي [MDN جدول](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table).
66
96
97
+
<<<<<<< HEAD
67
98
هذا أقل من معظم المعاملات وأكبر بقليل من `=` و `?`.
68
99
69
100
لذلك إذا أردنا استخدام `??` في تعبيرات معقدة نقوم بإضافة أقواس:
101
+
=======
102
+
So `??` is evaluated after most other operations, but before `=` and `?`.
103
+
104
+
If we need to choose a value with `??` in a complex expression, then consider adding parentheses:
105
+
>>>>>>> 69e44506c3e9dac74c282be37b55ba7ff122ae74
70
106
71
107
```js run
72
108
let height =null;
@@ -78,6 +114,7 @@ let area = (height ?? 100) * (width ?? 50);
78
114
alert(area); // 5000
79
115
```
80
116
117
+
<<<<<<< HEAD
81
118
إذا لم نستخدم الأقواس فإن `*` له أولوية أعلى وسينفذ أولًا كأننا كتبنا:
82
119
83
120
```js
@@ -86,19 +123,45 @@ let area = height ?? (100 * width) ?? 50;
86
123
```
87
124
88
125
هناك أيضًا قيود لغوية. لأسباب أمنية لا يمكن استخدام `??` مع `&&` أو `||`.
126
+
=======
127
+
Otherwise, if we omit parentheses, `*` has the higher precedence than `??` and would run first.
128
+
129
+
That would work be the same as:
130
+
131
+
```js
132
+
// probably not correct
133
+
let area = height ?? (100* width) ??50;
134
+
```
135
+
136
+
There's also a related language-level limitation.
137
+
138
+
**Due to safety reasons, it's forbidden to use `??` together with `&&` and `||` operators.**
139
+
>>>>>>> 69e44506c3e9dac74c282be37b55ba7ff122ae74
89
140
90
141
هذا سينتج خطأ لغوي:
91
142
92
143
```js run
93
144
let x =1&&2??3; // Syntax error
94
145
```
95
146
147
+
<<<<<<< HEAD
96
148
هذا القيد قد لا يبدو منطقيًا ولكن لبعض الأسباب تم إضافته للغة.
97
149
98
150
استخدم الأقواس لتجنب الخطأ:
99
151
100
152
```js run
101
153
let x = (1&&2) ??3; // تعمل دون مشاكل
154
+
=======
155
+
The limitation is surely debatable, but it was added to the language specification with the purpose to avoid programming mistakes, as people start to switch to `??` from `||`.
Copy file name to clipboardExpand all lines: 1-js/04-object-basics/07-optional-chaining/article.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -124,7 +124,7 @@ user2.admin?.();
124
124
125
125
Here, in both lines we first use the dot `.` to get `admin` property, because the user object must exist, so it's safe read from it.
126
126
127
-
Then `?.()` checks the left part: if the user exists, then it runs (for `user1`). Otherwise (for `user2`) the evaluation stops without errors.
127
+
Then `?.()` checks the left part: if the admin function exists, then it runs (for `user1`). Otherwise (for `user2`) the evaluation stops without errors.
128
128
129
129
The `?.[]` syntax also works, if we'd like to use brackets `[]` to access properties instead of dot `.`. Similar to previous cases, it allows to safely read a property from an object that may not exist.
0 commit comments