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/11-logical-operators/article.md
+33-30Lines changed: 33 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,94 +33,97 @@ Dacă un operant nu este de tip boolean, acesta este convertit automat într-unu
33
33
De exemplu, numărul „1” este tratat ca fiind „adevărat”, iar numărul „0” este „fals”:
34
34
35
35
```js run
36
-
if (1||0) { // funcționează asemenea lui if( adevărat || false )
36
+
if (1||0) { // funcționează asemenea lui if( adevărat || fals )
37
37
alert( 'efectiv adevărat!' );
38
38
}
39
39
```
40
40
41
-
Most of the time, OR `||`is used in an `if` statement to test if *any* of the given conditions is `true`.
41
+
De cele mai multe ori, semnul ORI `||`este folositi într-un if statement pentru a testa dacă vreauna dintre condiții este adevărată.
42
42
43
-
For example:
43
+
Spre exemplu:
44
44
45
45
```js run
46
46
let hour =9;
47
47
48
48
*!*
49
49
if (hour <10|| hour >18) {
50
50
*/!*
51
-
alert( 'The office is closed.' );
51
+
alert( 'Biroul este închis.' );
52
52
}
53
53
```
54
54
55
-
We can pass more conditions:
55
+
Putem adăuga mai multe condiții:
56
56
57
57
```js run
58
58
let hour =12;
59
59
let isWeekend =true;
60
60
61
61
if (hour <10|| hour >18|| isWeekend) {
62
-
alert( 'The office is closed.' ); //it is the weekend
62
+
alert( 'Biroul este închis.' ); //este weekend.
63
63
}
64
64
```
65
65
66
-
## OR "||" finds the first truthy value[#or-finds-the-first-truthy-value]
66
+
## ORI "||" identifică prima valoare efectiv adevărată[#or-finds-the-first-truthy-value]
67
67
68
-
The logic described above is somewhat classical. Now, let's bring in the "extra" features of JavaScript.
68
+
Logica descrisă mai sus este oarecum clasică. Haideți să discutăm despre calitățile „extra” din JavaScript.
69
69
70
-
The extended algorithm works as follows.
70
+
Algoritmul extinds funcționează după cum urmează.
71
71
72
-
Given multiple OR'ed values:
72
+
Sunt date mai multe valori ale lui ORI.
73
73
74
74
```js
75
75
result = value1 || value2 || value3;
76
76
```
77
77
78
-
The OR`||`operator does the following:
78
+
Operatorul ORI`||`face următoarele lucruri:
79
79
80
-
-Evaluates operands from left to right.
81
-
-For each operand, converts it to boolean. If the result is `true`, stops and returns the original value of that operand.
82
-
-If all operands have been evaluated (i.e. all were `false`), returns the last operand.
80
+
-Evaluzează operanții de la stânga spre dreapta.
81
+
-Fiecare operant este convertit într-o valoare de tip boolean. Dacă rezultatul este „adevărat”, execuția se oprește și valoarea originală a acelui operant este returnată.
82
+
-Dacă toți operanții au fost evaluați (iar toți erau falși), ultimul operant este returnat.
83
83
84
-
A value is returned in its original form, without the conversion.
84
+
O valoare este returnată în forma ei originală, fără conversiune.
85
85
86
-
In other words, a chain of OR`||`returns the first truthy value or the last one if no truthy value is found.
86
+
Cu alte cuvinte, într-un lanț de ORI`||`este returnată prima valoare efectiv adevărată sau ultima dacă nicio valoare efectiv adevărată nu este găsită.
87
87
88
-
For instance:
88
+
Spre exemplu:
89
89
90
90
```js run
91
-
alert( 1||0 ); // 1 (1 is truthy)
91
+
alert( 1||0 ); // 1 (1 este efectiv adevărat)
92
92
93
-
alert( null||1 ); // 1 (1 is the first truthy value)
94
-
alert( null||0||1 ); // 1 (the first truthy value)
93
+
alert( null||1 ); // 1 (1 este prima valoare efectiv adevărată)
94
+
alert( null||0||1 ); // 1 (prima valoare efectiv adevărată)
95
95
96
-
alert( undefined||null||0 ); // 0 (all falsy, returns the last value)
96
+
alert( undefined||null||0 ); // 0 (toate valorile sunt efectiv false, este returnată ultima valoare)
97
97
```
98
98
99
-
This leads to some interesting usage compared to a "pure, classical, boolean-only OR".
99
+
Asta conduce la o utilizate mai interesantă spre deosebire de un boolean clasic de tip ORI.
100
100
101
-
1.**Getting the first truthy value from a list of variables or expressions.**
101
+
1.**Poate fi obținută prima valoare efectiv adevărată dintr-o listă de variabile sau expresii.**
102
102
103
-
For instance, we have`firstName`, `lastName`and`nickName` variables, all optional (i.e. can be undefined or have falsy values).
103
+
Spre exemplu, avem variabilele`firstName`, `lastName`si`nickName`, toate opționale (adică pot fi undefined sau pot avea valori efectiv false)
104
104
105
-
Let's use OR `||`to choose the one that has the data and show it (or`"Anonymous"`if nothing set):
105
+
Hai să folosim operatorul ORI `||`pentru a selecta variabila care conține date și să le facem să apară (sau`"Anonim"`dacă niciuna nu are nicio valoare):
If all variables were falsy, `"Anonymous"` would show up.
117
+
<!-- If all variables were falsy, `"Anonymous"` would show up. -->
118
+
Dacă toate variabilele ar fi efectiv false, ar apărea textul `"Anonim"`.
118
119
119
-
2.**Short-circuit evaluation.**
120
+
2.**Evaulare de tip scurt circuit**
120
121
121
-
Another feature ofOR`||`operator is the so-called "short-circuit" evaluation.
122
+
O altă caracteristică a operatorului ORI`||`este așa numita evaluare de tip scurt circuit.
122
123
123
-
It means that `||` processes its arguments until the first truthy value is reached, and then the value is returned immediately, without even touching the other argument.
124
+
<!-- It means that `||` processes its arguments until the first truthy value is reached, and then the value is returned immediately, without even touching the other argument. -->
125
+
Aceasta înseamnă că `||` își procesează argumentele până când prima valoare efectiv adevărată este întâlnită, iar apoi acea valoare este returnată imediat, fără ca restul argumentelor să mai fie luate în considerate.
126
+
<!-- Mai îmbunătățește traducere de mai sus!-->
124
127
125
128
The importance ofthis feature becomes obvious if an operand isn't just a value, but an expression with a side effect, such as a variable assignment or a function call.
0 commit comments