Skip to content

Commit 89aea2e

Browse files
authored
Merge pull request #134 from matmont/feature/nullishCoalescingOperator
Update article.md
2 parents ec209e1 + b2c5cea commit 89aea2e

File tree

1 file changed

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

1 file changed

+45
-45
lines changed

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

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,50 @@
22

33
[recent browser="new"]
44

5-
Here, in this article, we'll say that an expression is "defined" when it's neither `null` nor `undefined`.
5+
Qui, in questo articolo, diremo che un'espressione è "definita" quando non è né `null` `undefined`.
66

7-
The nullish coalescing operator is written as two question marks `??`.
7+
Il nullish coalescing operator è scritto come due punti di domanda `??`.
88

9-
The result of `a ?? b` is:
10-
- if `a` is defined, then `a`,
11-
- if `a` isn't defined, then `b`.
9+
Il risultato di `a ?? b` è:
10+
- se `a` è definito, allora `a`,
11+
- se `a` non è definito, allora `b`.
1212

1313

14-
In other words, `??` returns the first argument if it's not `null/undefined`. Otherwise, the second one.
14+
In altre parole, `??` ritorna il primo argomento se questo non è `null/undefined`. Altrimenti, ritorna il secondo.
1515

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+
Il nullish coalescing operator non è qualcosa di completamente nuovo. È solo un modo più elegante (una migliore sintassi) per recuperare il primo valore "definito" dei due.
1717

18-
We can rewrite `result = a ?? b` using the operators that we already know, like this:
18+
Possiamo riscrivere `result = a ?? b` usando gli operatori che già conosciamo, nel seguente modo:
1919

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

24-
The common use case for `??` is to provide a default value for a potentially undefined variable.
24+
Un caso d'uso comune per l'operatore `??` è quello di fornire un valore di default per una variabile potenzialmente "non definita".
2525

26-
For example, here we show `Anonymous` if `user` isn't defined:
26+
Per esempio, qui mostriamo `Anonymous` se `user` non è definito:
2727

2828
```js run
2929
let user;
3030

3131
alert(user ?? "Anonymous"); // Anonymous
3232
```
3333
34-
Of course, if `user` had any value except `null/undefined`, then we would see it instead:
34+
Ovviamente, se `user` ha un qualsiasi valore eccetto `null/undefined`, allora vedremo quel valore:
3535
3636
```js run
3737
let user = "John";
3838

3939
alert(user ?? "Anonymous"); // 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+
Possiamo anche usare una sequenza di `??` per selezionare, da una lista, il primo valore che non sia `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+
Per esempio, supponiamo di avere dei dati relativi ad un utente nelle variabili `firstName`, `lastName` o `nickName`. Tutte queste potrebbero essere non definite, se l'utente dovesse decidere di non inserirne i valori.
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+
Vorremmo visualizzare il nome dell'utente usando una di queste variabili, oppure mostrare "Anonymous" se tutte queste sono indefinite.
4747
48-
Let's use the `??` operator for that:
48+
Usiamo l'operatore `??`per questo:
4949
5050
```js run
5151
let firstName = null;
@@ -58,11 +58,11 @@ alert(firstName ?? lastName ?? nickName ?? "Anonymous"); // Supercoder
5858
*/!*
5959
```
6060
61-
## Comparison with ||
61+
## Confronti con ||
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+
L'operatore OR `||` può essere usato nello stesso modo dell'operatore `??`, come descritto nel [capitolo precedente](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+
Per esempio, nel codice precedente potremmo rimpiazzare `??` con `||` e ottenere comunque il medesimo risultato:
6666
6767
```js run
6868
let firstName = null;
@@ -75,19 +75,19 @@ alert(firstName || lastName || nickName || "Anonymous"); // 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+
L'operatore OR `||` esiste sin dagli inizi di JavaScript, quindi gli sviluppatori lo hanno usato a tale scopo per molto tempo.
7979
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+
D'altro canto, il nullish coalescing operator `??` fu aggiunto in JavaScript recentemente, poichè le persone non erano del tutto felici dell'operatore `||`.
8181
82-
The important difference between them is that:
83-
- `||` returns the first *truthy* value.
84-
- `??` returns the first *defined* value.
82+
L'importante differenza tra essi è la seguente:
83+
- `||` ritorna il primo valore *truthy*.
84+
- `??` ritorna il primo valore *definito*.
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+
In altre parole, `||` non distingue tra `false`, `0`, una stringa vuota `""` e `null/undefined`. Sono tutti, allo stesso modo -- valori "falsy". Se uno tra questi è il primo argomento di `||`, allora otterremo il secondo argomento come risultato.
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+
In pratica, però, potremmo voler usare il valore di default solamente quando la variabile è `null/undefined`. È in questo caso che il valore è davvero sconosciuto/non settato.
8989
90-
For example, consider this:
90+
Per esempio, consideriamo questo:
9191
9292
```js run
9393
let height = 0;
@@ -96,18 +96,18 @@ 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+
- `height || 100` controlla se `height` possiede un valore "falsy", ed effettivamente così è,
100+
- quindi il risultato è il secondo argomento, `100`.
101+
- `height ?? 100` controlla se `height` è `null/undefined`, ma così non è,
102+
- quindi il risultato è `height` "così com'è", ovvero `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+
Se un'altezza pari a zero è un valore accettabile, allora questo non dovrebbe essere rimpiazzato con il valore di default, quindi `??` fa la cosa giusta.
105105
106-
## Precedence
106+
## Precedenza
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+
La precedenza dell'operatore `??` è piuttosto bassa: `5` nella [MDN table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table). Quindi `??` è valutato prima di `=` e `?`, ma dopo la maggior parte degli altri operatori, come `+`, `*`.
109109
110-
So if we'd like to choose a value with `??` in an expression with other operators, consider adding parentheses:
110+
Quindi, se volessimo scegliere un valore tramite l'operatore `??` in un'espressione contenente altri operatori, dovremmo considerare l'utilizzo delle parentesi:
111111
112112
```js run
113113
let height = null;
@@ -119,7 +119,7 @@ let area = (height ?? 100) * (width ?? 50);
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+
Altrimenti, se omettessimo le parentesi, siccome `*` ha una precedenza maggiore rispetto a `??`, sarebbe eseguito prima, portando a risultati scorretti.
123123
124124
```js
125125
// without parentheses
@@ -129,19 +129,19 @@ let area = height ?? 100 * width ?? 50;
129129
let area = height ?? (100 * width) ?? 50;
130130
```
131131
132-
### Using ?? with && or ||
132+
### Usare ?? con && o ||
133133
134-
Due to safety reasons, JavaScript forbids using `??` together with `&&` and `||` operators, unless the precedence is explicitly specified with parentheses.
134+
Per motivi di sicurezza, JavaScript proibisce l'utilizzo di `??` insieme agli operatori `&&` e `||`, a meno che la precedenza non sia esplicitamente specificata tramite l'utilizzo delle parentesi.
135135
136-
The code below triggers a syntax error:
136+
Il codice sottostante causa un errore di sintassi:
137137
138138
```js run
139139
let x = 1 && 2 ?? 3; // Syntax error
140140
```
141141
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 `||`.
142+
La limitazione è sicuramente discutibile, ma fu aggiunta alle specifiche del linguaggio con lo scopo di evitare errori di programmazione nel momento in cui le persone hanno iniziato ad utilizzare `??` al posto di `||`.
143143
144-
Use explicit parentheses to work around it:
144+
Per aggirare il problema si possono utilizzare delle parentesi esplicite:
145145
146146
```js run
147147
*!*
@@ -151,16 +151,16 @@ let x = (1 && 2) ?? 3; // Works
151151
alert(x); // 2
152152
```
153153
154-
## Summary
154+
## Sommario
155155
156-
- The nullish coalescing operator `??` provides a short way to choose the first "defined" value from a list.
156+
- Il nullish coalescing operator `??` fornisce una scorciatoia per la scelta del primo valore "definito" da una lista di valori.
157157
158-
It's used to assign default values to variables:
158+
È usato per assegnare valori di default alle variabili:
159159
160160
```js
161161
// set height=100, if height is null or undefined
162162
height = height ?? 100;
163163
```
164164
165-
- The operator `??` has a very low precedence, only a bit higher than `?` and `=`, so consider adding parentheses when using it in an expression.
166-
- It's forbidden to use it with `||` or `&&` without explicit parentheses.
165+
- L'operatore `??` ha una precedenza molto bassa, solo un po' più alta di `?` e `=`, quindi considerare l'aggiunta di parentesi quando lo si utilizza all'interno di un'espressione.
166+
- È proibito usarlo con `||` o `&&` senza l'utilizzo di parentesi esplicite.

0 commit comments

Comments
 (0)