|
1 | | -# Nullish coalescing operator '??' |
| 1 | +# Operator łączenia wartości null '??' |
2 | 2 |
|
3 | 3 | [recent browser="new"] |
4 | 4 |
|
5 | | -Here, in this article, we'll say that an expression is "defined" when it's neither `null` nor `undefined`. |
| 5 | +W tym artykule zakładamy, że wyrażenie jest "zdefiniowane", jeżeli nie jest `null` albo `undefined`. |
6 | 6 |
|
7 | | -The nullish coalescing operator is written as two question marks `??`. |
| 7 | +Operator łączenia wartości null zapisujemy jako dwa znaki zapytania `??`. |
8 | 8 |
|
9 | | -The result of `a ?? b` is: |
10 | | -- if `a` is defined, then `a`, |
11 | | -- if `a` isn't defined, then `b`. |
| 9 | +Wynikiem `a ?? b` jest: |
| 10 | +- jeżeli `a` jest zdefiniowane, to wynikiem jest `a`, |
| 11 | +- jeżeli `a` nie jest zdefiniowane, to wynikiem jest `b`. |
12 | 12 |
|
13 | 13 |
|
14 | | -In other words, `??` returns the first argument if it's not `null/undefined`. Otherwise, the second one. |
| 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 | | -The nullish coalescing operator 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 isn't anything completely new. It's just a nice syntax to get the first "defined" value of the two. |
17 | 17 |
|
18 | | -We can rewrite `result = a ?? b` using the operators that we already know, like this: |
| 18 | +Możemy zapisać `result = a ?? b` używając operatorów, które już znamy: |
19 | 19 |
|
20 | 20 | ```js |
21 | 21 | result = (a !== null && a !== undefined) ? a : b; |
22 | 22 | ``` |
23 | 23 |
|
24 | | -The common use case for `??` is to provide a default value for a potentially undefined variable. |
| 24 | +Typowym przykładem użycia `??` jest dostarczenie domyślnej wartości dla potencjalnie niezdefiniowanej zmiennej. |
25 | 25 |
|
26 | | -For example, here we show `Anonymous` if `user` isn't defined: |
| 26 | +Dla przykładu, wyświetlamy `Anonimowy`, jeżeli zmienna `user` jest niezdefiniowana: |
27 | 27 |
|
28 | 28 | ```js run |
29 | 29 | let user; |
30 | 30 |
|
31 | | -alert(user ?? "Anonymous"); // Anonymous |
| 31 | +alert(user ?? "Anonimowy"); // Anonimowy |
32 | 32 | ``` |
33 | 33 |
|
34 | | -Of course, if `user` had any value except `null/undefined`, then we would see it instead: |
| 34 | +Oczywiście, jeżeli zmienna `user` ma inną wartość niż `null/undefined`, wtedy oczywiście powinniśmy zobaczyć jej wartość: |
35 | 35 |
|
36 | 36 | ```js run |
37 | 37 | let user = "John"; |
38 | 38 |
|
39 | | -alert(user ?? "Anonymous"); // John |
| 39 | +alert(user ?? "Anonimowy"); // John |
40 | 40 | ``` |
41 | 41 |
|
42 | 42 | We can also use a sequence of `??` to select the first value from a list that isn't `null/undefined`. |
@@ -77,7 +77,7 @@ alert(firstName || lastName || nickName || "Anonymous"); // Supercoder |
77 | 77 |
|
78 | 78 | The OR `||` operator exists since the beginning of JavaScript, so developers were using it for such purposes for a long time. |
79 | 79 |
|
80 | | -On the other hand, the nullish coalescing operator `??` was added to JavaScript only recently, and the reason for that was that people weren't quite happy with `||`. |
| 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 `||`. |
81 | 81 |
|
82 | 82 | The important difference between them is that: |
83 | 83 | - `||` returns the first *truthy* value. |
@@ -153,7 +153,7 @@ alert(x); // 2 |
153 | 153 |
|
154 | 154 | ## Summary |
155 | 155 |
|
156 | | -- The nullish coalescing operator `??` provides a short way to choose the first "defined" value from a list. |
| 156 | +- Operator łączenia wartości null `??` provides a short way to choose the first "defined" value from a list. |
157 | 157 |
|
158 | 158 | It's used to assign default values to variables: |
159 | 159 |
|
|
0 commit comments