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/12-nullish-coalescing-operator/article.md
+40-40Lines changed: 40 additions & 40 deletions
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ Wynikiem `a ?? b` jest:
13
13
14
14
Innymi słowy, `??` zwraca pierwszy argument jeżeli jego wartość jest inna niż `null/undefined`. W przeciwnym razie, zwraca drugi argument.
15
15
16
-
Operator łączenia wartości null isn't anything completely new. It's just a nice syntax to get the first "defined" value of the two.
16
+
Operator łączenia wartości null nie jest całkiem nowy. Jest to po prostu ładna składnia, aby dostać pierwszą zdefiniowaną wartość z dwóch dostępnych.
17
17
18
18
Możemy zapisać `result = a ?? b` używając operatorów, które już znamy:
19
19
@@ -23,71 +23,71 @@ result = (a !== null && a !== undefined) ? a : b;
23
23
24
24
Typowym przykładem użycia `??` jest dostarczenie domyślnej wartości dla potencjalnie niezdefiniowanej zmiennej.
25
25
26
-
Dla przykładu, wyświetlamy `Anonimowy`, jeżeli zmienna `user` jest niezdefiniowana:
26
+
Dla przykładu, wyświetlamy `Anonim`, jeżeli zmienna `user` jest niezdefiniowana:
27
27
28
28
```js run
29
29
let user;
30
30
31
-
alert(user ??"Anonimowy"); //Anonimowy
31
+
alert(user ??"Anonim"); //Anonim
32
32
```
33
33
34
-
Oczywiście, jeżeli zmienna `user` ma inną wartość niż `null/undefined`, wtedy oczywiście powinniśmy zobaczyć jej wartość:
34
+
Oczywiście, jeżeli zmienna `user` ma inną wartość niż `null/undefined`, wtedy powinniśmy zobaczyć jej wartość:
35
35
36
36
```js run
37
37
let user ="John";
38
38
39
-
alert(user ??"Anonimowy"); // John
39
+
alert(user ??"Anonim"); // John
40
40
```
41
41
42
-
We can also use a sequence of `??` to select the first value from a list that isn't`null/undefined`.
42
+
Możemy również użyć sekwencji `??`, żeby wybrać pierwszą wartość z listy, która jest inna niż`null/undefined`.
43
43
44
-
Let's say we have a user's data in variables `firstName`, `lastName`or`nickName`. All of them may be undefined, if the user decided not to enter a value.
44
+
Powiedzmy, że mamy dane użytkownika w zmiennej `firstName`, `lastName`oraz`nickName`. Wszystkie mogą być niezdefiniowane, jeżeli użytkownik zdecyduje się ich nie wprowadzać.
45
45
46
-
We'd like to display the user name using one of these variables, or show "Anonymous" if all of them are undefined.
46
+
Chcielibyśmy wyświetlić nazwę użytkownika użyuwając jednej z tych zmiennych, albo wyświetlić "Anonim", jeżeli wszystkie są niezdefiniowane.
The OR `||`operator can be used in the same way as `??`, as it was described in the [previous chapter](info:logical-operators#or-finds-the-first-truthy-value).
63
+
Operator OR `||`może być użyty w ten sam sposób co `??`, jak to było opisane w [poprzednim rozdziale](info:logical-operators#or-finds-the-first-truthy-value).
64
64
65
-
For example, in the code above we could replace `??`with`||`and still get the same result:
65
+
Dla przykładu, w kodzie powyżej, możemy zastąpić `??`z`||`i wciąż otrzymać ten sam rezultat:
The OR `||`operator exists since the beginning of JavaScript, so developers were using it for such purposes for a long time.
78
+
Operator OR `||`istnieje od początku w JavaScript, więc był w ten sposób używany przez developerów od bardzo dawna.
79
79
80
-
On the other hand, Operator łączenia wartości null `??`was added to JavaScript only recently, and the reason for that was that people weren't quite happy with`||`.
80
+
Z drugiej strony, Operator łączenia wartości null `??`został dodany do JavaScript ostatnio i powodem było to, że ludzie nie byli całkiem zadowoleni z`||`.
81
81
82
-
The important difference between them is that:
83
-
- `||`returns the first *truthy* value.
84
-
- `??`returns the first *defined* value.
82
+
Ważną różnicą pomiędzy nimi jest:
83
+
- `||`zwraca pierwszą *truthy* wartość.
84
+
- `??`zwraca pierwszą *defined* wartość.
85
85
86
-
In other words, `||`doesn't distinguish between`false`, `0`, an empty string `""`and`null/undefined`. They are all the same -- falsy values. If any of these is the first argument of`||`, then we'll get the second argument as the result.
86
+
Innymi słowy, `||`nie rozróżnia pomiędzy`false`, `0`, pustym stringiem `""`i`null/undefined`. Wszystkie one są takie same -- falsy wartości. Jeżeli którakolwiek z tych wartości jest pierwszym argumentem w`||`, wtedy otrzymamy drugi argument jako wynik.
87
87
88
-
In practice though, we may want to use default value only when the variable is `null/undefined`. That is, when the value is really unknown/not set.
88
+
W praktyce, możemy chcieć użyć domyślnej wartości tylko wtedy jeżeli zmienna ma wartość `null/undefined`. To znaczy tylko wtedy kiedy wartość naprawdę jest nieznana/nie ustawiona.
89
89
90
-
For example, consider this:
90
+
Na przykład, rozważmy:
91
91
92
92
```js run
93
93
let height =0;
@@ -96,47 +96,47 @@ alert(height || 100); // 100
96
96
alert(height ??100); // 0
97
97
```
98
98
99
-
- The`height ||100`checks`height`for being a falsy value, and it really is.
100
-
- so the result is the second argument, `100`.
101
-
- The`height ??100`checks`height`for being`null/undefined`, and it's not,
102
-
- so the result is `height` "as is", that is`0`.
99
+
- Wyrażenie`height ||100`sprawdza`height`pod kątem falsy wartości, i jest ona taka.
100
+
- w takim razie wynikiem jest drugi argument, `100`.
101
+
- Wyrażenie`height ??100`sprawdza`height`pod kątem`null/undefined`, i nie jest,
102
+
- w takim razie, wynikiem jest `height` "takie jakie jest", zatem`0`.
103
103
104
-
If the zero height is a valid value, that shouldn't be replaced with the default, then`??`does just the right thing.
104
+
Jeżeli zerowa wysokość jest poprawną wartością, która nie powinna być zastąpiona wartością domyślną, wtedy`??`sprawdzi się doskonale.
105
105
106
-
## Precedence
106
+
## Precedence /
107
107
108
-
The precedence of the `??`operator is rather low: `5`in the [MDN table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table). So`??`is evaluated before`=`and`?`, but after most other operations, such as`+`, `*`.
108
+
Priorytet operatora `??`jest raczej niski: `5`[tabela MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table). Więc`??`jest przetwarzane przed`=`i`?`, ale po większości innych operatorów, jak`+`, `*`.
109
109
110
-
So if we'd like to choose a value with `??`in an expression with other operators, consider adding parentheses:
110
+
Więc jeżeli chcemy wybrać wartość używając `??`w wyrażeniu z innymi operatorami, rozważ dodanie nawiasów:
111
111
112
112
```js run
113
113
let height =null;
114
114
let width =null;
115
115
116
-
//important: use parentheses
116
+
//ważne: użyj nawiasów
117
117
let area = (height ??100) * (width ??50);
118
118
119
119
alert(area); // 5000
120
120
```
121
121
122
-
Otherwise, if we omit parentheses, then as `*`has the higher precedence than `??`, it would execute first, leading to incorrect results.
122
+
W innym wypadku, jeżeli ominiemy nawiasy, wtedy `*`ma większy priorytet niż `??`, wykona się najpierw, prowadząc do niewłaściwych wyników.
123
123
124
124
```js
125
-
//without parentheses
125
+
//bez nawiasów
126
126
let area = height ??100* width ??50;
127
127
128
-
// ...works the same as this (probably not what we want):
128
+
// ...działa tak samo (prawdopodobnie nie tego chcemy):
129
129
let area = height ?? (100* width) ??50;
130
130
```
131
131
132
-
### Using ?? with && or ||
132
+
### Użycie ?? z && albo ||
133
133
134
-
Due to safety reasons, JavaScript forbids using`??`together with `&&`and`||` operators, unless the precedence is explicitly specified with parentheses.
134
+
Z powodów bezpieczeństwa, JavaScript zabrania użycia`??`razem z operatorami `&&`i`||`, chyba, że priorytety są zdefiniowane dokładnie z użyciem nawiasów.
135
135
136
-
The code below triggers a syntax error:
136
+
Kod poniżej wywołuje błąd składni:
137
137
138
138
```js run
139
-
let x =1&&2??3; //Syntax error
139
+
let x =1&&2??3; //Błąd składni
140
140
```
141
141
142
142
The limitation is surely debatable, but it was added to the language specification with the purpose to avoid programming mistakes, when people start to switch to `??` from `||`.
0 commit comments