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
-يساوي: `a == b` ، يرجى ملاحظة أن علامة المساواة المزدوجة` = `تعني اختبار المساواة ، في حين أن كلمة واحدة`a = b`تعني تعيين أو مساواة .
10
+
-لا تساوي. في الرياضيات ، يكون الترميز في JavaScript مكتوب مثل a !=b
11
11
12
-
In this article we'll learn more about different types of comparisons, how JavaScript makes them, including important peculiarities.
12
+
في هذه المقالة سنتعلم المزيد عن الأنواع المختلفة من المقارنات ، وكيف تجعلها JavaScript ، بما في ذلك الخصائص المهمة.
13
13
14
-
## Boolean is the result
14
+
## Boolean هي النتيجة
15
15
16
-
All comparison operators return a boolean value:
16
+
تُرجع جميع عوامل المقارنة قيمة منطقية:
17
17
18
-
-`true` -- means "yes", "correct" or "the truth".
19
-
-`false` -- means "no", "wrong" or "not the truth".
18
+
-"صحيح" - يعني "نعم" أو "صحيح" أو "الحقيقة".
19
+
-"false" - تعني "لا" أو "خطأ" أو "ليست الحقيقة".
20
20
21
-
For example:
21
+
فمثلا:
22
22
23
23
```js run
24
24
alert( 2>1 ); // true (correct)
25
25
alert( 2==1 ); // false (wrong)
26
26
alert( 2!=1 ); // true (correct)
27
27
```
28
28
29
-
A comparison result can be assigned to a variable, just like any value:
29
+
يمكن تعيين نتيجة مقارنة لمتغير ، تمامًا مثل أي قيمة:
30
30
31
31
```js run
32
32
let result =5>4; // assign the result of the comparison
33
33
alert( result ); // true
34
34
```
35
35
36
-
## String comparison
36
+
## مقارنة الكلمات
37
37
38
-
To see whether a string is greater than another, JavaScript uses the so-called "dictionary" or "lexicographical" order.
38
+
لمعرفة ما إذا كانت السلسلة أكبر من غيرها ، تستخدم جافا سكريبت ما يسمى بترتيب "القاموس" أو "المعجم".
39
39
40
-
In other words, strings are compared letter-by-letter.
40
+
بمعنى آخر ، تتم مقارنة السلاسل حرفًا بحرف.
41
41
42
-
For example:
42
+
فمثلا:
43
43
44
44
```js run
45
45
alert( 'Z'>'A' ); // true
46
46
alert( 'Glow'>'Glee' ); // true
47
47
alert( 'Bee'>'Be' ); // true
48
48
```
49
49
50
-
The algorithm to compare two strings is simple:
50
+
خوارزمية مقارنة سلسلتين بسيطة:
51
51
52
-
1.Compare the first character of both strings.
53
-
2.If the first character from the first string is greater (or less) than the other string's, then the first string is greater (or less) than the second. We're done.
54
-
3.Otherwise, if both strings' first characters are the same, compare the second characters the same way.
55
-
4.Repeat until the end of either string.
56
-
5.If both strings end at the same length, then they are equal. Otherwise, the longer string is greater.
52
+
1.قارن الحرف الأول من كلا السلاسل.
53
+
2.إذا كان الحرف الأول من السلسلة الأولى أكبر (أو أقل) من السلسلة الأخرى ، فإن السلسلة الأولى أكبر (أو أقل) من السلسلة الثانية. لقد انتهينا.
54
+
3.وبخلاف ذلك ، إذا كانت الأحرف الأولى لكلتا السلاسل متشابهة ، قارن الأحرف الثانية بنفس الطريقة.
55
+
4.كرر حتى نهاية أي سلسلة.
56
+
5.إذا انتهى كلا السلاسل بنفس الطول ، فإنهما متساويان. خلاف ذلك ، فإن السلسلة الأطول أكبر.
57
57
58
-
In the examples above, the comparison `'Z' > 'A'` gets to a result at the first step while the strings `"Glow"` and `"Glee"` are compared character-by-character:
58
+
في الأمثلة أعلاه ، تصل المقارنة "Z"> "A" إلى نتيجة في الخطوة الأولى بينما تتم مقارنة السلاسل "" Glow "و" Glee "حرفًا بحرف:
59
59
60
-
1.`G` is the same as `G`.
61
-
2.`l`is the same as `l`.
62
-
3.`o`is greater than `e`. Stop here. The first string is greater.
60
+
1."G" هو نفسه "G".
61
+
2- "l`هي نفس" l`.
62
+
3.`o`أكبر من`e`. توقف هنا. السلسلة الأولى أكبر.
63
63
64
-
```smart header="Not a real dictionary, but Unicode order"
65
-
The comparison algorithm given above is roughly equivalent to the one used in dictionaries or phone books, but it's not exactly the same.
64
+
"``عنوان ذكي ومختصر =" ليس قاموسًا حقيقيًا ، ولكن ترتيب Unicode "
65
+
خوارزمية المقارنة الواردة أعلاه تكافئ تقريبًا تلك المستخدمة في القواميس أو دفاتر الهاتف ، ولكنها ليست هي نفسها تمامًا.
66
66
67
-
For instance, case matters. A capital letter `"A"` is not equal to the lowercase `"a"`. Which one is greater? The lowercase `"a"`. Why? Because the lowercase character has a greater index in the internal encoding table JavaScript uses (Unicode). We'll get back to specific details and consequences of this in the chapter <info:string>.
68
-
```
67
+
على سبيل المثال ، القضية مهمة. الحرف الكبير "A" لا يساوي الأحرف الصغيرة "a". أيهما أكبر؟ الأحرف الصغيرة "" أ "". لماذا ا؟ لأن الحرف الصغير يحتوي على فهرس أكبر في جدول الترميز الداخلي تستخدم JavaScript (Unicode). سنعود إلى تفاصيل محددة وعواقب ذلك في الفصل <info:string>.
68
+
``
69
69
70
-
## Comparison of different types
70
+
## مقارنة الأنواع المختلفة
71
71
72
-
When comparing values of different types, JavaScript converts the values to numbers.
72
+
عند مقارنة قيم الأنواع المختلفة ، تحوّل JavaScript القيم إلى أرقام.
73
73
74
-
For example:
74
+
فمثلا:
75
75
76
76
```js run
77
77
alert( '2'>1 ); // true, string '2' becomes a number 2
78
78
alert( '01'==1 ); // true, string '01' becomes a number 1
79
79
```
80
80
81
-
For boolean values, `true` becomes `1` and `false` becomes `0`.
81
+
بالنسبة إلى القيم المنطقية ، يصبح "true" "1" ويصبح "false" "0".
82
82
83
-
For example:
83
+
فمثلا:
84
84
85
85
```js run
86
86
alert( true==1 ); // true
87
87
alert( false==0 ); // true
88
88
```
89
89
90
-
````smart header="A funny consequence"
91
-
It is possible that at the same time:
92
-
93
-
- Two values are equal.
94
-
- One of them is `true` as a boolean and the other one is `false` as a boolean.
90
+
"" عنوان ذكي ومختصر = "نتيجة مضحكة"
91
+
من الممكن أنه في نفس الوقت:
95
92
96
-
For example:
93
+
- قيمتان متساويتان.
94
+
- واحد منهم "صحيح" باعتباره منطقيًا والآخر "خطأ" باعتباره منطقيًا.
From JavaScript's standpoint, this result is quite normal. An equality check converts values using the numeric conversion (hence `"0"` becomes `0`), while the explicit `Boolean` conversion uses another set of rules.
109
-
````
110
107
111
-
## Strict equality
108
+
هذه النتيجة طبيعية من وجهة نظر JavaScript. يؤدي التحقق من المساواة إلى تحويل القيم باستخدام التحويل الرقمي (وبالتالي يصبح "0" `يُصبح` 0`) ، بينما يستخدم التحويل الصريح `Boolean` مجموعة أخرى من القواعد.
109
+
````
110
+
111
+
## المساواة الصارمة
112
+
113
+
هناك مشكلة في التحقق المنتظم من المساواة `= =`. لا يمكن التمييز بين "0" و "false":
112
114
113
-
A regular equality check `==` has a problem. It cannot differentiate `0` from `false`:
114
115
115
116
```js run
116
117
alert( 0==false ); // true
117
118
```
118
119
119
-
The same thing happens with an empty string:
120
+
يحدث الشيء نفسه بسلسلة فارغة:
120
121
121
122
```js run
122
123
alert( ''==false ); // true
123
124
```
125
+
يحدث هذا لأن المعاملات من أنواع مختلفة يتم تحويلها إلى أرقام من قبل عامل المساواة `==`. السلسلة الفارغة ، تمامًا مثل `false` ، تصبح صفرًا.
124
126
125
-
This happens because operands of different types are converted to numbers by the equality operator `==`. An empty string, just like `false`, becomes a zero.
126
-
127
-
What to do if we'd like to differentiate `0` from `false`?
127
+
ماذا أفعل إذا أردنا التمييز بين "0" و "false"؟
128
128
129
-
**A strict equality operator`===` checks the equality without type conversion.**
129
+
** عامل المساواة الصارم`= =` يتحقق من المساواة بدون تحويل النوع. **
130
130
131
-
In other words, if `a` and `b` are of different types, then `a === b` immediately returns `false` without an attempt to convert them.
131
+
بمعنى آخر ، إذا كان "a" و "b" من أنواع مختلفة ، فإن "a === b" يُرجع على الفور "خطأ" دون محاولة تحويلها.
132
132
133
-
Let's try it:
133
+
فلنجربها:
134
134
135
135
```js run
136
136
alert( 0===false ); // false, because the types are different
137
137
```
138
138
139
-
There is also a "strict non-equality" operator `!==`analogous to `!=`.
139
+
هناك أيضًا عامل "صارم لعدم المساواة" `! ==`مشابه لـ '! = `.
140
140
141
-
The strict equality operator is a bit longer to write, but makes it obvious what's going on and leaves less room for errors.
141
+
عامل المساواة الصارم أطول قليلاً في الكتابة ، لكنه يوضح ما يحدث ويترك مساحة أقل للأخطاء.
142
142
143
-
## Comparison with null and undefined
143
+
## مقارنة مع لا شيء وغير محدد
144
144
145
-
There's a non-intuitive behavior when `null` or `undefined` are compared to other values.
145
+
هناك سلوك غير بديهي عند مقارنة "null" أو "undefined" بقيم أخرى.
146
146
147
-
For a strict equality check `===`
148
-
: These values are different, because each of them is a different type.
147
+
للتحقق من المساواة الصارمة `= =`
148
+
: هذه القيم مختلفة ، لأن كل منها نوع مختلف.
149
149
150
150
```js run
151
151
alert( null === undefined ); // false
152
152
```
153
153
154
-
For a non-strict check `==`
155
-
: There's a special rule. These two are a "sweet couple": they equal each other (in the sense of `==`), but not any other value.
154
+
للتحقق غير الصارم `= =`
155
+
: هناك قاعدة خاصة. هذان الزوجان "زوجان لطيفان": إنهما يساويان بعضهما البعض (بمعنى `= =`) ، ولكن ليس أي قيمة أخرى.
156
156
157
157
```js run
158
158
alert( null == undefined ); // true
159
159
```
160
160
161
-
For maths and other comparisons `< > <= >=`
162
-
: `null/undefined` are converted to numbers: `null` becomes `0`, while `undefined` becomes `NaN`.
163
161
164
-
Now let's see some funny things that happen when we apply these rules. And, what's more important, how to not fall into a trap with them.
162
+
بالنسبة للرياضيات والمقارنات الأخرى `<> <=> =`
163
+
: "null / undefined" يتم تحويلها إلى أرقام: "null" تصبح "0" ، بينما "undefined" تصبح "NaN".
164
+
165
+
الآن دعونا نرى بعض الأشياء المضحكة التي تحدث عندما نطبق هذه القواعد. والأهم من ذلك ، كيف لا تقع في فخ معهم.
165
166
166
-
### Strange result: null vs 0
167
+
### نتيجة غريبة: صفر مقابل 0
167
168
168
-
Let's compare`null`with a zero:
169
+
دعنا نقارن`null`بصفر:
169
170
170
171
```js run
171
172
alert( null>0 ); // (1) false
172
173
alert( null==0 ); // (2) false
173
174
alert( null>=0 ); // (3) *!*true*/!*
174
175
```
175
176
176
-
Mathematically, that's strange. The last result states that "`null` is greater than or equal to zero", so in one of the comparisons above it must be `true`, but they are both false.
177
+
رياضيا ، هذا غريب. تشير النتيجة الأخيرة إلى أن "القيمة" فارغة "أكبر من الصفر أو تساويه" ، لذا في إحدى المقارنات أعلاه ، يجب أن تكون "true" ، لكن كلاهما خطأ.
177
178
178
-
The reason is that an equality check `==`and comparisons `> < >= <=`work differently. Comparisons convert `null`to a number, treating it as `0`. That's why (3) `null >= 0`is true and (1)`null > 0`is false.
179
+
والسبب هو أن التحقق من المساواة `= =`والمقارنات `>> <> = <=`يعمل بشكل مختلف. تقوم المقارنات بتحويل `null`إلى رقم ، وتعاملها على أنها`0`. لهذا السبب (3) `null> = 0`صحيحة و (1)` null> 0`خاطئة.
179
180
180
-
On the other hand, the equality check `==`for`undefined`and `null`is defined such that, without any conversions, they equal each other and don't equal anything else. That's why (2) `null == 0`is false.
181
+
من ناحية أخرى ، يتم تحديد علامة المساواة `= =`لـ`undefined`و`null`بحيث لا يساوي أحدهما الآخر دون تحويلات ولا يساوي أي شيء آخر. لهذا السبب (2) `null == 0`خطأ.
181
182
182
-
### An incomparable undefined
183
+
### غير المحدد غير المقارن
183
184
184
-
The value `undefined`shouldn't be compared to other values:
185
+
لا يجب مقارنة القيمة `undefined`بالقيم الأخرى:
185
186
186
187
```js run
187
188
alert( undefined>0 ); // false (1)
188
189
alert( undefined<0 ); // false (2)
189
190
alert( undefined==0 ); // false (3)
190
191
```
191
192
192
-
Why does it dislike zero so much? Always false!
193
+
لماذا يكره الصفر كثيرا؟ دائما خطأ!
193
194
194
-
We get these results because:
195
+
نحصل على هذه النتائج للأسباب التالية:
195
196
196
-
-Comparisons`(1)`and`(2)`return`false`because `undefined`gets converted to `NaN`and `NaN`is a special numeric value which returns `false`for all comparisons.
197
-
-The equality check `(3)`returns`false`because `undefined`only equals`null`, `undefined`, and no other value.
197
+
-المقارنات`(1)`و`(2)`إرجاع`false`لأنه يتم تحويل`undefined`إلى `NaN`و`NaN`هي قيمة رقمية خاصة تُرجع `false`لجميع المقارنات.
198
+
-تحقق المساواة `((3)`تُرجع`false`لأن`undefined`تساوي فقط`null` و`undefined` ولا قيمة أخرى.
198
199
199
-
### Evade problems
200
+
### تجنب المشاكل
200
201
201
-
Why did we go over these examples? Should we remember these peculiarities all the time? Well, not really. Actually, these tricky things will gradually become familiar over time, but there's a solid way to evade problems with them:
202
+
لماذا راجعنا هذه الأمثلة؟ هل يجب أن نتذكر هذه الخصائص المميزة طوال الوقت؟ حسنًا ، ليس حقًا. في الواقع ، ستصبح هذه الأشياء الصعبة مألوفة تدريجيًا بمرور الوقت ، ولكن هناك طريقة صلبة للتهرب من المشاكل معها:
202
203
203
-
Just treat any comparison with `undefined/null` except the strict equality `===`with exceptional care.
204
+
فقط تعامل مع أي مقارنة بـ "undefined / null" باستثناء المساواة الصارمة `===`مع رعاية استثنائية.
204
205
205
-
Don't use comparisons`>= > <<=`with a variable which may be `null/undefined`, unless you're really sure of what you're doing. If a variable can have these values, check for them separately.
206
+
لا تستخدم المقارنات`> => <<=`مع متغير قد يكون `فارغًا / غير محدد` ، ما لم تكن متأكدًا حقًا مما تفعله. إذا كان المتغير يمكن أن يكون له هذه القيم ، تحقق منها بشكل منفصل.
206
207
207
-
## Summary
208
+
## ملخص
208
209
209
-
-Comparison operators return a boolean value.
210
-
-Strings are compared letter-by-letter in the "dictionary" order.
211
-
-When values of different types are compared, they get converted to numbers (with the exclusion of a strict equality check).
212
-
-The values `null` and `undefined` equal`==`each other and do not equal any other value.
213
-
-Be careful when using comparisons like`>`or`<`with variables that can occasionally be `null/undefined`. Checking for `null/undefined` separately is a good idea.
210
+
-ترجع عوامل المقارنة قيمة منطقية.
211
+
-تتم مقارنة السلاسل حرفًا بحرف في ترتيب "القاموس".
212
+
-عند مقارنة القيم من أنواع مختلفة ، يتم تحويلها إلى أرقام (باستثناء التحقق من المساواة الصارم).
213
+
-القيمتان "null" و "undefined" تساوي`==`بعضها البعض ولا تساوي أي قيمة أخرى.
214
+
-كن حذرًا عند استخدام مقارنات مثل`>`أو`<`مع المتغيرات التي يمكن أن تكون أحيانًا `خالية / غير محددة '. يُعد التحقق من "null / undefined" بشكل منفصل فكرة جيدة.
0 commit comments