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
-Greater/less than or equals: <code>a >= b</code>, <code>a <= b</code>.
7
-
-Equals: `a == b` (please note the double equals sign`=`. A single symbol `a = b`would mean an assignment).
8
-
-Not equals. In maths the notation is <code>≠</code>, but in JavaScript it's written as an assignment with an exclamation sign before it: <code>a != b</code>.
-Większe/mniejsze niż lub równe: <code>a >= b</code>, <code>a <= b</code>.
7
+
-Równe: `a == b` (zauważ, że jest tutaj podwójny znak`=`. Pojedyncze użycie `a = b`oznacza przypisanie).
8
+
-Nierówne. W matematyce zapiszemy to jako <code>≠</code>, ale w JavaScript jest to zapisane jako wykrzyknik przed znakiem równości: <code>a != b</code>.
9
9
10
-
## Boolean is the result
10
+
## Wynikiem jest Boolean
11
11
12
-
Like all other operators, a comparison returns a value. In this case, the value is a boolean.
12
+
Jak wszystkie inne operatory porównanie zwraca wartość. W tym przypadku wartością jest Boolean.
13
13
14
14
-`true` -- means "yes", "correct" or "the truth".
15
15
-`false` -- means "no", "wrong" or "not the truth".
16
16
17
-
For example:
17
+
Na przykład:
18
18
19
19
```js run
20
-
alert( 2>1 ); // true (correct)
21
-
alert( 2==1 ); // false (wrong)
22
-
alert( 2!=1 ); // true (correct)
20
+
alert( 2>1 ); // true (prawda)
21
+
alert( 2==1 ); // false (fałsz)
22
+
alert( 2!=1 ); // true (prawda)
23
23
```
24
24
25
-
A comparison result can be assigned to a variable, just like any value:
25
+
Wynik porównania może być przypisany do zmiennej, jak każda inna wartość:
26
26
27
27
```js run
28
-
let result =5>4; //assign the result of the comparison
28
+
let result =5>4; //przypisz wynik porównania
29
29
alert( result ); // true
30
30
```
31
31
32
-
## String comparison
32
+
## Porównanie stringów
33
33
34
-
To see whether a string is greater than another, JavaScript uses the so-called "dictionary" or "lexicographical" order.
34
+
Aby zobaczyć czy ciąg znaków jest większy niż inny JavaScript używa porównania, które nazywamy "słownikowym" lub "leksykograficznym".
35
35
36
-
In other words, strings are compared letter-by-letter.
36
+
Innymi słowy, stringi porównywane są litera po literze.
37
37
38
-
For example:
38
+
Na przykład:
39
39
40
40
```js run
41
41
alert( 'Z'>'A' ); // true
42
-
alert( 'Glow'>'Glee' ); // true
43
-
alert( 'Bee'>'Be' ); // true
42
+
alert( 'Brat'>'Brak' ); // true
43
+
alert( 'Jan'>'Ja' ); // true
44
44
```
45
45
46
-
The algorithm to compare two strings is simple:
46
+
Algorytm porównuje dwa stringi w prosty sposób:
47
47
48
-
1.Compare the first character of both strings.
49
-
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.
50
-
3.Otherwise, if both strings' first characters are the same, compare the second characters the same way.
51
-
4.Repeat until the end of either string.
52
-
5.If both strings end at the same length, then they are equal. Otherwise, the longer string is greater.
48
+
1.Porównaj pierwszy znak w obu stringach.
49
+
2.Jeśli pierwszy znak w pierwszym stringu jest większy (lub mniejszy) niż inny string, wtedy pierwszy string jest większy (lub mniejszy). Porównanie zakończone.
50
+
3.Jeśli pierwsze znaki są takie same zrób porównanie dla kolejnego znaku w ten sam sposób jak w punkcie nr 2.
51
+
4.Powtarzaj dopóki nie dojdzie do końca stringu.
52
+
5.Jeśli oba stringi mają taką samą długość są równe. W przeciwnym przypadku dłuższy string jest większy.
53
53
54
-
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:
54
+
W powyższych przypadkach porównanie `'Z' > 'A'`zwróci rezultat w pierwszym podejściu. Porównanie `"Brat"`z`"Brak"`będzie porównywane znak po znaku:
55
55
56
-
1.`G` is the same as `G`.
57
-
2.`l` is the same as `l`.
58
-
3.`o` is greater than `e`. Stop here. The first string is greater.
56
+
1.`B` jest takie same jak `B`.
57
+
2.`r` jest takie same jak `r`.
58
+
3.`a` jest takie same jak `a`.
59
+
3.`t` jest większe niż `k`. Zatrzymaj tutaj. Pierwszy string jest większy.
59
60
60
-
```smart header="Not a real dictionary, but Unicode order"
61
-
The comparison algorithm given above is roughly equivalent to the one used in dictionaries or phone books, but it's not exactly the same.
61
+
```smart header="Nie do końca słownikowa, bo kolejność wg Unicode"
62
+
Podany powyżej przykład jest prawie taki sam jak algorytm używany w słownikach lub książkach telefonicznych. Ale nie jest dokładnie taki sam.
62
63
63
-
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>.
64
+
Na przykład wielkość ma znaczenie. Duża litera `"A"` nie jest równa małej literze `"a"`. Która jest większa? Mała litera `"a"`. Dlaczego? Ponieważ małe litery mają większy index w wewnętrznej tabeli kodowania znaków (Unicode), której używa JavaScript. Wrócimy do tego w rozdziale <info:string>.
64
65
```
65
66
66
-
## Comparison of different types
67
+
## Porównania wartości różnego typu
67
68
68
-
When comparing values of different types, JavaScript converts the values to numbers.
69
+
Kiedy porównujemy wartości różnego typu JavaScript konwertuje te wartości na liczby.
69
70
70
-
For example:
71
+
Na przykład:
71
72
72
73
```js run
73
-
alert( '2'>1 ); // true, string '2' becomes a number 2
74
-
alert( '01'==1 ); // true, string '01' becomes a number 1
74
+
alert( '2'>1 ); // true, string '2' staje się numerem 2
75
+
alert( '01'==1 ); // true, string '01' staje się numerem 1
75
76
```
76
77
77
-
For boolean values,`true`becomes `1` and`false`becomes`0`.
78
+
Dla wartości Boolean`true`staje się `1`, a`false`staje się`0`.
78
79
79
-
For example:
80
+
Na przykład:
80
81
81
82
```js run
82
83
alert( true==1 ); // true
83
84
alert( false==0 ); // true
84
85
```
85
86
86
-
````smart header="A funny consequence"
87
-
It is possible that at the same time:
87
+
````smart header="Zabawna zależność"
88
+
Jest możliwe, aby w tym samym czasie:
88
89
89
-
- Two values are equal.
90
-
- One of them is `true` as a boolean and the other one is `false` as a boolean.
90
+
- Dwie wartości były równe.
91
+
- Jedna z nich będzie `true` jako Boolean, natomiast druga jest `false` jako Boolean.
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.
105
+
Z punkty widzenia JavaScript taki rezultat jest oczekiwany i normalny. Porównanie konwertuje wartości na typ liczbowy (więc string `"0"` zostaje `0`), podczas gdy porównanie `Boolean` konwertuje te wartości w inny sposób.
105
106
````
106
107
107
-
## Strict equality
108
+
## Operator identyczności
108
109
109
-
A regular equality check `==`has a problem. It cannot differentiate`0`from`false`:
110
+
Operator równości `==`ma jedną wadę. Nie potrafi odróżnić`0`od`false`.
110
111
111
112
```js run
112
113
alert( 0==false ); // true
113
114
```
114
115
115
-
The same thing happens with an empty string:
116
+
To samo się stanie gdy porównamy pusty string:
116
117
117
118
```js run
118
119
alert( ''==false ); // true
119
120
```
120
121
121
-
This happens because operands of different types are converted to numbers by the equality operator `==`. An empty string, just like`false`, becomes a zero.
122
+
Dzieje się tak, ponieważ operandy różnych typów są konwertowane do typu liczbowego podczas użycia `==`. Pusty string, a także`false` stają się 0.
122
123
123
-
What to do if we'd like to differentiate `0`from`false`?
124
+
Co powinniśmy zrobić, aby odróżnić `0`od`false`?
124
125
125
-
**A strict equality operator `===`checks the equality without type conversion.**
126
+
**Operator identyczności `===`sprawdza równość bez konwersji typu.**
126
127
127
-
In other words, if`a`and`b`are of different types, then `a === b`immediately returns`false`without an attempt to convert them.
128
+
Innymi słowy, jeśli`a`i`b`są różnego typu wtedy `a === b`natychmiastowo zwróci`false`bez próby ich wcześniejszej konwersji.
128
129
129
-
Let's try it:
130
+
Spróbujmy więc:
130
131
131
132
```js run
132
-
alert( 0===false ); // false, because the types are different
133
+
alert( 0===false ); // false, ponieważ typy są różne
133
134
```
134
135
135
-
There is also a "strict non-equality" operator `!==`analogous to`!=`.
136
+
Istnieje również "operator nieidentyczności" `!==`analogiczny do`!=`.
136
137
137
-
The strict equality operator is a bit longer to write, but makes it obvious what's going on and leaves less room for errors.
138
+
Operator identyczności jest nieco dłuższy do zapisania, ale czyni porównanie oczywistym i nie zostawia miejsca na błędy.
138
139
139
-
## Comparison with null and undefined
140
+
## Porównania z null i undefined
140
141
141
-
Let's see more edge cases.
142
+
Zobaczmy kilka skrajnych przypadków.
142
143
143
-
There's a non-intuitive behavior when `null`or`undefined`are compared to other values.
144
+
Nie jest intuicyjne w jaki sposób zachowają się `null`lub`undefined`gdy będą porównywane z innymi wartościami.
144
145
145
146
146
-
For a strict equality check`===`
147
-
: These values are different, because each of them is a different type.
147
+
Dla sprawdzenia identyczności`===`
148
+
: Te wartości są różne, ponieważ każda jest innego typu.
148
149
149
150
```js run
150
151
alert( null === undefined ); // false
151
152
```
152
153
153
-
For a non-strict check`==`
154
-
: 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
+
Dla sprawdzenia równości`==`
155
+
: Istnieje specjalna reguła. Te dwie wartości są "słodką parą": są równe sobie (w sensie `==`), ale nie są równe innej wartości.
155
156
156
157
```js run
157
158
alert( null == undefined ); // true
158
159
```
159
160
160
-
For maths and other comparisons`< > <= >=`
161
-
: `null/undefined`are converted to numbers: `null`becomes `0`, while`undefined`becomes`NaN`.
161
+
W matematyce i innych porównaniach`< > <= >=`
162
+
: `null/undefined`są skonwertowane do liczb: `null`staje się `0`, natomiast`undefined`staje się`NaN`.
162
163
163
-
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.
164
+
Zobaczmy kilka ciekawych rzeczy, które się dzieją gdy zaaplikujemy te reguły. I co jest najważniejsze, jak nie wpaść z nimi w tarapaty.
164
165
165
-
### Strange result: null vs 0
166
+
### Dziwny rezultat: null vs 0
166
167
167
-
Let's compare `null`with a zero:
168
+
Porównajmy `null`z zerem:
168
169
169
170
```js run
170
171
alert( null>0 ); // (1) false
171
172
alert( null==0 ); // (2) false
172
173
alert( null>=0 ); // (3) *!*true*/!*
173
174
```
174
175
175
-
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.
176
+
W matematyce jest to dziwne. Ostatni rezultat, w którym "`null`jest większe lub równe zero" zwraca `true`, podczas gdy oba wcześniejsze zwracają `false`, wydaje się, że również powinno być `false`, a jest `true`.
176
177
177
-
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.
178
+
Powodem takiego wyniku jest to, że znak`==`i porównania`> < >= <=`nie działają w ten sam sposób. Porównania konwertują`null`do liczby traktując go jako `0`. Dlatego właśnie (3) `null >= 0`jest true i (1) `null > 0`jest false.
178
179
179
-
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.
180
+
Z drugiej strony użycie `==`dla`undefined`oraz`null`jest zdefiniowane bez żadnych konwersji i są równe tylko sobie i niczemu innemu. I właśnie dlatego w przykładzie (2) `null == 0`jest false.
180
181
181
-
### An incomparable undefined
182
+
### Nieporównywalny undefined
182
183
183
-
The value `undefined`shouldn't be compared to other values:
184
+
Wartość `undefined`nie powinna być porównywana z innymi wartościami:
184
185
185
186
```js run
186
187
alert( undefined>0 ); // false (1)
187
188
alert( undefined<0 ); // false (2)
188
189
alert( undefined==0 ); // false (3)
189
190
```
190
191
191
-
Why does it dislike zero so much? Always false!
192
+
Dlaczego nie lubi nawet zera? Bo zawsze jest false!
192
193
193
-
We get these results because:
194
+
Dostaliśmy takie rezultaty ponieważ:
194
195
195
-
-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.
196
-
-The equality check `(3)`returns`false`because`undefined`only equals `null`, `undefined`, and no other value.
196
+
-Porównanie`(1)`i`(2)`zwraca`false`ponieważ`undefined`zostaje skonwertowane do`NaN`i`NaN`jest specjalną numeryczną wartością, która zawsze zwraca`false`dla wszystkich porównań.
197
+
-Sprawdzanie równości `(3)`zwraca`false`ponieważ`undefined`jest równe tylko `null`, `undefined` i żadnej innej wartości.
197
198
198
-
### Evade problems
199
+
### Unikanie problemów
199
200
200
-
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:
201
+
Dlaczego w ogóle przeszliśmy przez te przykłady? Czy powinniśmy pamiętać o tych osobliwych rzeczach cały czas? Nie do końca. Tak właściwie to te podstępne rzeczy staną się jasne z czasem, ale jest jeden porządny sposób na uniknięcie związanych z nimi problemów:
201
202
202
-
Just treat any comparison with `undefined/null`except the strict equality `===`with exceptional care.
203
+
Po prostu traktuj każde porównanie z `undefined/null`używając znaku identyczności `===`zachowując wszelkie środki ostrożności.
203
204
204
-
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.
205
+
Nie używaj porównań`>= > < <=`ze zmiennymi, które mogą być `null/undefined`. Chyba że wiesz co robisz. Jeśli zmienna może mieć te wartości sprawdź je oddzielnie.
205
206
206
-
## Summary
207
+
## Podsumowanie
207
208
208
-
-Comparison operators return a boolean value.
209
-
-Strings are compared letter-by-letter in the "dictionary" order.
210
-
-When values of different types are compared, they get converted to numbers (with the exclusion of a strict equality check).
211
-
-The values `null`and`undefined`equal `==`each other and do not equal any other value.
212
-
-Be careful when using comparisons like `>`or`<`with variables that can occasionally be `null/undefined`. Checking for `null/undefined`separately is a good idea.
209
+
-Operatory porównania zwracają wartość typu Boolean (true lub false).
210
+
-Stringi porównywane są litera po literze w "słownikowej" kolejności.
211
+
-Jeśli porównujemy wartości różnych typów, zostaną one skonwertowane do liczby (chyba, że użyjemy operatora identyczności).
212
+
-Wartości `null`i`undefined`są równe sobie `==`i są różne od każdej innej wartości.
213
+
-Bądź ostrożny gdy używasz porównac takich jak `>`lub`<`ze zmiennymi, które mogą być `null/undefined`. Oddzielne sprawdzanie dla `null/undefined`jest dobrym rozwiązaniem.
0 commit comments