Skip to content

Commit 9f9fcfd

Browse files
authored
Translation of the article "Nulish coalescing operator '??'"
Translation of the article "Nulish coalescing operator '??'"
2 parents 9636af9 + 2573336 commit 9f9fcfd

File tree

1 file changed

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

1 file changed

+61
-61
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,94 @@
1-
# Nullish coalescing operator '??'
1+
# Nulinio susiliejimo operatorius '??'
22

33
[recent browser="new"]
44

5-
The nullish coalescing operator is written as two question marks `??`.
5+
Nulinio susiliejimo operatorius užrašomas dviem klausiamaisiais ženklais `??`.
66

7-
As it treats `null` and `undefined` similarly, we'll use a special term here, in this article. We'll say that an expression is "defined" when it's neither `null` nor `undefined`.
7+
Kadangi jis apdoroja `null` ir `undefined` vienodai, šiame straipsnyje įvesime specialų terminą. Trumpai sakysime, kad vertė yra “apibrėžta”, jei ji nėra lygi nei `null`, nei `undefined`.
88

9-
The result of `a ?? b` is:
10-
- if `a` is defined, then `a`,
11-
- if `a` isn't defined, then `b`.
9+
Rezultatas `a ?? b` yra:
10+
- jei `a` yra apibrėžta, tada `a`,
11+
- jei `a` yra neapibrėžta, tada `b`.
1212

13-
In other words, `??` returns the first argument if it's not `null/undefined`. Otherwise, the second one.
13+
Kitaip tariant, `??` grąžina pirmąjį argumentą, jei jis nėra `null/undefined`. Priešingu atveju - antrąjį.
1414

15-
The nullish coalescing operator isn't anything completely new. It's just a nice syntax to get the first "defined" value of the two.
15+
Nulinio susiliejimo operatorius nėra visiškai naujas. Tai tik graži sintaksė, leidžianti gauti pirmąją “apibrėžtą” vertę iš dviejų.
1616

17-
We can rewrite `result = a ?? b` using the operators that we already know, like this:
17+
Mes galime perrašyti `result = a ?? b`, naudodami jau mums žinomus operatorius, pavyzdžiui, taip:
1818

1919
```js
2020
result = (a !== null && a !== undefined) ? a : b;
2121
```
2222

23-
Now it should be absolutely clear what `??` does. Let's see where it helps.
23+
Dabar turėtų būti visiškai aišku, ką daro `??`. Pažiūrėkime, kur jis padeda.
2424

25-
The common use case for `??` is to provide a default value for a potentially undefined variable.
25+
Paprastai operatorius `??` reikalingas norint nustatyti numatytoją vertę potencialiai neapibrėžtam kintamajam.
2626

27-
For example, here we show `user` if defined, otherwise `Anonymous`:
27+
Pavyzdžiui, čia mes atvaizduojame `user`, jei jo vertė nėra `null/undefined`, priešingu atveju - Anonimas:
2828

2929
```js run
3030
let user;
3131

32-
alert(user ?? "Anonymous"); // Anonymous (user not defined)
32+
alert(user ?? "Anonimas"); // Anonimas (user yra neapibrėžtas)
3333
```
3434
35-
Here's the example with `user` assigned to a name:
35+
O štai pavyzdys, kai `user` priskirta vertė:
3636
3737
```js run
38-
let user = "John";
38+
let user = "Jonas";
3939

40-
alert(user ?? "Anonymous"); // John (user defined)
40+
alert(user ?? "Anonimas"); // Jonas (user yra apibrėžtas)
4141
```
4242
43-
We can also use a sequence of `??` to select the first value from a list that isn't `null/undefined`.
43+
Mes taip pat galime naudoti `??` seką, norėdami iš sąrašo išrinkti pirmąją vertę, kuri nėra `null/undefined`.
4444
45-
Let's say we have a user's data in variables `firstName`, `lastName` or `nickName`. All of them may be not defined, if the user decided not to enter a value.
45+
Tarkime, kad turime naudotojo duomenis kintamuosiuose `firstName`, `lastName` arba `nickName`. Visi jie gali būti neapibrėžti, jei naudotojas nusprendė neįvesti vertės.
4646
47-
We'd like to display the user name using one of these variables, or show "Anonymous" if all of them aren't defined.
47+
Mes norėtume, kad naudotojo vardas būtų atvaizduojamas naudojant vieną iš šių kintamųjų arba rodomas "Anonimas", jei visi jie neapibrėžti.
4848
49-
Let's use the `??` operator for that:
49+
Panaudokime operatorių `??`:
5050
5151
```js run
5252
let firstName = null;
5353
let lastName = null;
5454
let nickName = "Supercoder";
5555

56-
// shows the first defined value:
56+
// parodo pirmąją apibrėžtą vertę:
5757
*!*
58-
alert(firstName ?? lastName ?? nickName ?? "Anonymous"); // Supercoder
58+
alert(firstName ?? lastName ?? nickName ?? "Anonimas"); // Supercoder
5959
*/!*
6060
```
6161
62-
## Comparison with ||
62+
## Palyginimas su ||
6363
64-
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).
64+
Operatorius ARBA `||` gali būti naudojamas taip pat kaip ir `??`, kaip aprašyta [ankstesniame skyriuje](info:logical-operators#or-finds-the-first-truthy-value).
6565
66-
For example, in the code above we could replace `??` with `||` and still get the same result:
66+
Pavyzdžiui, aukščiau pateiktame kode galėtume pakeisti `??` į `||` ir vis tiek gautume tą patį rezultatą:
6767
6868
```js run
6969
let firstName = null;
7070
let lastName = null;
7171
let nickName = "Supercoder";
7272

73-
// shows the first truthy value:
73+
// parodo pirmąją truthy vertę:
7474
*!*
75-
alert(firstName || lastName || nickName || "Anonymous"); // Supercoder
75+
alert(firstName || lastName || nickName || "Anonimas"); // Supercoder
7676
*/!*
7777
```
7878
79-
Historically, the OR `||` operator was there first. It exists since the beginning of JavaScript, so developers were using it for such purposes for a long time.
79+
Istoriškai ARBA `||` operatorius buvo pirmasis. Jis egzistuoja nuo pat JavaScript atsiradimo pradžios, todėl programišiai jau seniai jį naudojo tokiems tikslams.
8080
81-
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 `||`.
81+
Kita vertus, nulinio susiliejimo operatorius `??` į JavaScript buvo pridėtas visai neseniai, o priežastis buvo ta, kad žmonės nebuvo patenkinti `||`.
8282
83-
The important difference between them is that:
84-
- `||` returns the first *truthy* value.
85-
- `??` returns the first *defined* value.
83+
Svarbus skirtumas tarp jų yra tas, kad:
84+
- `||` grąžina pirmąją *truthy* vertę.
85+
- `??` grąžina pirmąją *apibrėžtą* vertę
8686
87-
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.
87+
Kitaip tariant, `||` neskiria `false`, `0`, tuščios eilutės `""` ir `null/undefined`. Jos visos yra vienodos -- falsy vertės. Jei bet kuri iš jų yra pirmasis `||` argumentas, kaip rezultatą gausime antrąjį argumentą.
8888
89-
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.
89+
Tačiau praktikoje numatytąją vertę galime norėti naudoti tik tada, kai kintamasis yra `null/undefined`. T. y. kai vertė iš tikrųjų nežinoma/nenustatyta.
9090
91-
For example, consider this:
91+
Pavyzdžiui, panagrinėkite štai ką:
9292
9393
```js run
9494
let height = 0;
@@ -97,73 +97,73 @@ alert(height || 100); // 100
9797
alert(height ?? 100); // 0
9898
```
9999
100-
- The `height || 100` checks `height` for being a falsy value, and it's `0`, falsy indeed.
101-
- so the result of `||` is the second argument, `100`.
102-
- The `height ?? 100` checks `height` for being `null/undefined`, and it's not,
103-
- so the result is `height` "as is", that is `0`.
100+
- Naudojant `height || 100` patikrinama, ar `height` nėra falsy vertė, ir ji yra `0`, taigi tikrai falsy.
101+
- taigi `||` rezultatas yra antrasis argumentas, `100`.
102+
- Naudojant `height ?? 100` patikrinama, ar `height` nėra `null/undefined`, bet taip nėra,
103+
- taigi rezultatas yra pats kintamasis `height`, t. y. `0`
104104
105-
In practice, the zero height is often a valid value, that shouldn't be replaced with the default. So `??` does just the right thing.
105+
Praktikoje nulinis aukštis dažnai yra tinkama vertė, kurios nereikėtų keisti numatytąja. Taigi `??` daro teisingą dalyką.
106106
107-
## Precedence
107+
## Pirmenybė
108108
109-
The precedence of the `??` operator is the same as `||`. They both equal `4` in the [MDN table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table).
109+
Operatoriaus `??` pirmenybė yra tokia pati kaip ir `||`. Abu jie [MDN lentelėje](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table) yra lygūs `4`.
110110
111-
That means that, just like `||`, the nullish coalescing operator `??` is evaluated before `=` and `?`, but after most other operations, such as `+`, `*`.
111+
Tai reiškia, kad, kaip ir `||`, nulinio susiliejimo operatorius `??` vertinamas prieš `=` ir `?`, bet po daugumos kitų operacijų, tokių kaip `+`, `*`.
112112
113-
So if we'd like to choose a value with `??` in an expression with other operators, consider adding parentheses:
113+
Taigi, jei norime pasirinkti vertę su `??` išraiškoje su kitais operatoriais, pridėkite skliaustelius:
114114
115115
```js run
116116
let height = null;
117117
let width = null;
118118

119-
// important: use parentheses
119+
// svarbu: naudokite skliaustelius
120120
let area = (height ?? 100) * (width ?? 50);
121121

122122
alert(area); // 5000
123123
```
124124
125-
Otherwise, if we omit parentheses, then as `*` has the higher precedence than `??`, it would execute first, leading to incorrect results.
125+
Priešingu atveju, jei praleisime skliaustelius, kadangi `*` turi didesnę pirmenybę nei `??`, jis bus vykdomas pirmas, todėl rezultatai bus neteisingi.
126126
127127
```js
128-
// without parentheses
128+
// be skliaustelių
129129
let area = height ?? 100 * width ?? 50;
130130

131-
// ...works the same as this (probably not what we want):
131+
// ...veikia taip pat, kaip ir čia (tikriausiai ne tai, ko norime):
132132
let area = height ?? (100 * width) ?? 50;
133133
```
134134
135-
### Using ?? with && or ||
135+
### Naudojimas ?? kartu su && arba ||
136136
137-
Due to safety reasons, JavaScript forbids using `??` together with `&&` and `||` operators, unless the precedence is explicitly specified with parentheses.
137+
Dėl saugumo priežasčių JavaScript draudžia naudoti `??` kartu su `&&` ir `||` operatoriais, nebent pirmenybė būtų aiškiai nurodyta skliaustuose.
138138
139-
The code below triggers a syntax error:
139+
Toliau pateiktas kodas sukelia sintaksės klaidą:
140140
141141
```js run
142-
let x = 1 && 2 ?? 3; // Syntax error
142+
let x = 1 && 2 ?? 3; // Sintaksės klaida
143143
```
144144
145-
The limitation is surely debatable, it was added to the language specification with the purpose to avoid programming mistakes, when people start to switch from `||` to `??`.
145+
Šis apribojimas tikrai yra ginčytinas, jis buvo pridėtas į kalbos specifikaciją siekiant išvengti programavimo klaidų, kai žmonės pradeda pereiti nuo `||` prie `??`.
146146
147-
Use explicit parentheses to work around it:
147+
Norėdami tai apeiti, naudokite aiškius skliaustelius:
148148
149149
```js run
150150
*!*
151-
let x = (1 && 2) ?? 3; // Works
151+
let x = (1 && 2) ?? 3; // Veikia
152152
*/!*
153153

154154
alert(x); // 2
155155
```
156156
157-
## Summary
157+
## Santrauka
158158
159-
- The nullish coalescing operator `??` provides a short way to choose the first "defined" value from a list.
159+
- Nulinio susiliejimo operatorius `??` yra trumpas būdas pasirinkti pirmąją “apibrėžtą” vertę iš sąrašo.
160160
161-
It's used to assign default values to variables:
161+
Jis naudojamas kintamiesiems priskirti numatytąsias vertes:
162162
163163
```js
164-
// set height=100, if height is null or undefined
164+
// nustatyti height=100, jei height lygus null arba undefined
165165
height = height ?? 100;
166166
```
167167
168-
- The operator `??` has a very low precedence, only a bit higher than `?` and `=`, so consider adding parentheses when using it in an expression.
169-
- It's forbidden to use it with `||` or `&&` without explicit parentheses.
168+
- Operatoriaus `??` pirmenybė yra labai maža, tik šiek tiek didesnė nei `?` ir `=`, todėl, naudodami jį išraiškoje, pridėkite skliaustelius.
169+
- Draudžiama naudoti `??` su `||` arba `&&` be aiškių skliaustelių.

0 commit comments

Comments
 (0)