Skip to content

Commit 14c2d04

Browse files
Bartłomiej Kalembasculpt0r
Bartłomiej Kalemba
authored andcommitted
WIP translation part
1 parent 37f9093 commit 14c2d04

File tree

1 file changed

+40
-40
lines changed
  • 1-js/02-first-steps/12-nullish-coalescing-operator

1 file changed

+40
-40
lines changed

1-js/02-first-steps/12-nullish-coalescing-operator/article.md

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Wynikiem `a ?? b` jest:
1313

1414
Innymi słowy, `??` zwraca pierwszy argument jeżeli jego wartość jest inna niż `null/undefined`. W przeciwnym razie, zwraca drugi argument.
1515

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.
1717

1818
Możemy zapisać `result = a ?? b` używając operatorów, które już znamy:
1919

@@ -23,71 +23,71 @@ result = (a !== null && a !== undefined) ? a : b;
2323

2424
Typowym przykładem użycia `??` jest dostarczenie domyślnej wartości dla potencjalnie niezdefiniowanej zmiennej.
2525

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:
2727

2828
```js run
2929
let user;
3030

31-
alert(user ?? "Anonimowy"); // Anonimowy
31+
alert(user ?? "Anonim"); // Anonim
3232
```
3333
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ść:
3535
3636
```js run
3737
let user = "John";
3838

39-
alert(user ?? "Anonimowy"); // John
39+
alert(user ?? "Anonim"); // John
4040
```
4141
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`.
4343
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ć.
4545
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.
4747
48-
Let's use the `??` operator for that:
48+
Użyjmy do tego operatora `??`:
4949
5050
```js run
5151
let firstName = null;
5252
let lastName = null;
5353
let nickName = "Supercoder";
5454

55-
// shows the first defined value:
55+
// pokazuje pierwszą zdefiniowaną wartość:
5656
*!*
57-
alert(firstName ?? lastName ?? nickName ?? "Anonymous"); // Supercoder
57+
alert(firstName ?? lastName ?? nickName ?? "Anonim"); // Supercoder
5858
*/!*
5959
```
6060
61-
## Comparison with ||
61+
## Porównanie z ||
6262
63-
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).
6464
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:
6666
6767
```js run
6868
let firstName = null;
6969
let lastName = null;
7070
let nickName = "Supercoder";
7171

72-
// shows the first truthy value:
72+
// pokazuje pierwszą truthy wartość:
7373
*!*
74-
alert(firstName || lastName || nickName || "Anonymous"); // Supercoder
74+
alert(firstName || lastName || nickName || "Anonim"); // Supercoder
7575
*/!*
7676
```
7777
78-
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.
7979
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 `||`.
8181
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ść.
8585
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.
8787
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.
8989
90-
For example, consider this:
90+
Na przykład, rozważmy:
9191
9292
```js run
9393
let height = 0;
@@ -96,47 +96,47 @@ alert(height || 100); // 100
9696
alert(height ?? 100); // 0
9797
```
9898
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`.
103103
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.
105105
106-
## Precedence
106+
## Precedence /
107107
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 `+`, `*`.
109109
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:
111111
112112
```js run
113113
let height = null;
114114
let width = null;
115115

116-
// important: use parentheses
116+
// ważne: użyj nawiasów
117117
let area = (height ?? 100) * (width ?? 50);
118118

119119
alert(area); // 5000
120120
```
121121
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.
123123
124124
```js
125-
// without parentheses
125+
// bez nawiasów
126126
let area = height ?? 100 * width ?? 50;
127127

128-
// ...works the same as this (probably not what we want):
128+
// ...działa tak samo (prawdopodobnie nie tego chcemy):
129129
let area = height ?? (100 * width) ?? 50;
130130
```
131131
132-
### Using ?? with && or ||
132+
### Użycie ?? z && albo ||
133133
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.
135135
136-
The code below triggers a syntax error:
136+
Kod poniżej wywołuje błąd składni:
137137
138138
```js run
139-
let x = 1 && 2 ?? 3; // Syntax error
139+
let x = 1 && 2 ?? 3; // Błąd składni
140140
```
141141
142142
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

Comments
 (0)