|
1 | | -# Type Conversions |
| 1 | +# Conversii de tip |
2 | 2 |
|
3 | | -Most of the time, operators and functions automatically convert the values given to them to the right type. |
| 3 | +Majoritatea timpului, operatorii si funcțiile convertesc automat valorile date catre tipul corect. |
4 | 4 |
|
5 | | -For example, `alert` automatically converts any value to a string to show it. Mathematical operations convert values to numbers. |
| 5 | +De exemplu, `alert` convertește automat orice valoare catre un șir pentru a o arăta. Operațiile matematice convertesc valorile catre numere. |
6 | 6 |
|
7 | | -There are also cases when we need to explicitly convert a value to the expected type. |
| 7 | +Există și cazuri unde este nevoie sa convertim explicit o valoare către tipul așteptat. |
8 | 8 |
|
9 | 9 | ```smart header="Not talking about objects yet" |
10 | | -In this chapter, we won't cover objects. For now we'll just be talking about primitives. |
| 10 | +În acest captiol, nu vom acoperi obiectele. Pentru moment vom vorbi doar despre primitive. |
11 | 11 |
|
12 | | -Later, after we learn about objects, in the chapter <info:object-toprimitive> we'll see how objects fit in. |
| 12 | +Mai târziu, dupa ce vom învăța despre obiecte, în acest capitol <info:object-toprimitive> vom vedea cum se incadrează obiectele. |
13 | 13 | ``` |
14 | 14 |
|
15 | | -## String Conversion |
| 15 | +## Conversia unui Șir |
16 | 16 |
|
17 | | -String conversion happens when we need the string form of a value. |
| 17 | +Conversia unui șir se întâmplă când avem nevoie de forma de șir a unei valori. |
18 | 18 |
|
19 | | -For example, `alert(value)` does it to show the value. |
| 19 | +De exemplu, `alert(value)` o face pentru a arăta valoarea. |
20 | 20 |
|
21 | | -We can also call the `String(value)` function to convert a value to a string: |
| 21 | +Totodată, putem apela funcța `String(value)` pentru a converti o valoare către un șir |
22 | 22 |
|
23 | 23 | ```js run |
24 | 24 | let value = true; |
25 | 25 | alert(typeof value); // boolean |
26 | 26 |
|
27 | 27 | *!* |
28 | | -value = String(value); // now value is a string "true" |
29 | | -alert(typeof value); // string |
| 28 | +value = String(value); // acum valoarea este un "adevărat" șir |
| 29 | +alert(typeof value); // șir |
30 | 30 | */!* |
31 | 31 | ``` |
32 | 32 |
|
33 | | -String conversion is mostly obvious. A `false` becomes `"false"`, `null` becomes `"null"`, etc. |
| 33 | +Conversia unui șir este destul de evidentă. Un `false` devine `"false"`, `null` devine `"null"`, etc. |
34 | 34 |
|
35 | | -## Numeric Conversion |
| 35 | +## Conversia Numerică |
36 | 36 |
|
37 | | -Numeric conversion happens in mathematical functions and expressions automatically. |
| 37 | +Conversia numerică se petrece in funcții și expresii matematice automat. |
38 | 38 |
|
39 | | -For example, when division `/` is applied to non-numbers: |
| 39 | +De exemplu, când împărțirea `/` este aplicată non-numerelor: |
40 | 40 |
|
41 | 41 | ```js run |
42 | | -alert( "6" / "2" ); // 3, strings are converted to numbers |
| 42 | +alert( "6" / "2" ); // 3, șirurile sunt convertite către numere |
43 | 43 | ``` |
44 | 44 |
|
45 | | -We can use the `Number(value)` function to explicitly convert a `value` to a number: |
| 45 | +Putem folosi funcția `Number(value)` pentru a converti explicit o `value` către un număr. |
46 | 46 |
|
47 | 47 | ```js run |
48 | 48 | let str = "123"; |
49 | | -alert(typeof str); // string |
| 49 | +alert(typeof str); // șir |
50 | 50 |
|
51 | | -let num = Number(str); // becomes a number 123 |
| 51 | +let num = Number(str); // devine un număr 123 |
52 | 52 |
|
53 | | -alert(typeof num); // number |
| 53 | +alert(typeof num); // număr |
54 | 54 | ``` |
55 | 55 |
|
56 | | -Explicit conversion is usually required when we read a value from a string-based source like a text form but expect a number to be entered. |
| 56 | +Conversia explicită de obicei este cerută când citim o valoarea dintr-o sursă bazată pe un șir ca și un formular text dar așteaptă un număr să fie inclus. |
57 | 57 |
|
58 | | -If the string is not a valid number, the result of such a conversion is `NaN`. For instance: |
| 58 | +Dacă șirul nu este un număr valid, rezultatul acestei conversii este `NaN`. De exemplu: |
59 | 59 |
|
60 | 60 | ```js run |
61 | | -let age = Number("an arbitrary string instead of a number"); |
| 61 | +let age = Number("un șir arbitrar in locul unui număr"); |
62 | 62 |
|
63 | | -alert(age); // NaN, conversion failed |
| 63 | +alert(age); // NaN, conversie eșuată |
64 | 64 | ``` |
65 | 65 |
|
66 | | -Numeric conversion rules: |
| 66 | +Regulile conversiei numerice: |
67 | 67 |
|
68 | | -| Value | Becomes... | |
| 68 | +| Valoare | Devine... | |
69 | 69 | |-------|-------------| |
70 | 70 | |`undefined`|`NaN`| |
71 | 71 | |`null`|`0`| |
72 | 72 | |<code>true and false</code> | `1` and `0` | |
73 | | -| `string` | Whitespaces from the start and end are removed. If the remaining string is empty, the result is `0`. Otherwise, the number is "read" from the string. An error gives `NaN`. | |
| 73 | +| `string` | Spațiile goale de la ïnceput și sfârșit sunt eliminate. Daca șirul rămas este gol, rezultatul este `0`. Altfel numărul este "citit" din șir . O eroare transmite `NaN`. | |
74 | 74 |
|
75 | | -Examples: |
| 75 | +Exemple: |
76 | 76 |
|
77 | 77 | ```js run |
78 | 78 | alert( Number(" 123 ") ); // 123 |
79 | | -alert( Number("123z") ); // NaN (error reading a number at "z") |
| 79 | +alert( Number("123z") ); // NaN (eroare la citirea unui numar la "z") |
80 | 80 | alert( Number(true) ); // 1 |
81 | 81 | alert( Number(false) ); // 0 |
82 | 82 | ``` |
83 | 83 |
|
84 | | -Please note that `null` and `undefined` behave differently here: `null` becomes zero while `undefined` becomes `NaN`. |
| 84 | +Va rugăm sa notați faptul că `null` și `undefined` se comportă diferit aici: `null` devine 0, în timp ce `undefined` devine `NaN`. |
85 | 85 |
|
86 | | -Most mathematical operators also perform such conversion, we'll see that in the next chapter. |
| 86 | +Cei mai mulți operatori matematici execută o astfel de conversie, vom vedea aceasta in capitolul următor. |
87 | 87 |
|
88 | 88 | ## Boolean Conversion |
| 89 | +## Conversia Boolean |
89 | 90 |
|
90 | | -Boolean conversion is the simplest one. |
| 91 | +Conversia boolean este cea mai simplă. |
91 | 92 |
|
92 | | -It happens in logical operations (later we'll meet condition tests and other similar things) but can also be performed explicitly with a call to `Boolean(value)`. |
| 93 | +Are loc in operațiile logice (mai tarziu vom cunoaște teste condiționale si alte lucruri similare) dar poate fi executată specific cu o apelare la `Boolean(value)`. |
93 | 94 |
|
94 | | -The conversion rule: |
| 95 | +Regula conversiei: |
95 | 96 |
|
96 | | -- Values that are intuitively "empty", like `0`, an empty string, `null`, `undefined`, and `NaN`, become `false`. |
97 | | -- Other values become `true`. |
| 97 | +- Valorile care sunt "goale" intuitiv, ca și `0`, un șir gol, `null`, `undefined`, and `NaN`, devin `false`. |
| 98 | +- Alte valori devin `true`. |
98 | 99 |
|
99 | | -For instance: |
| 100 | +De exemplu: |
100 | 101 |
|
101 | 102 | ```js run |
102 | 103 | alert( Boolean(1) ); // true |
103 | 104 | alert( Boolean(0) ); // false |
104 | 105 |
|
105 | | -alert( Boolean("hello") ); // true |
| 106 | +alert( Boolean("salut") ); // true |
106 | 107 | alert( Boolean("") ); // false |
107 | 108 | ``` |
108 | 109 |
|
109 | 110 | ````warn header="Please note: the string with zero `\"0\"` is `true`" |
110 | | -Some languages (namely PHP) treat `"0"` as `false`. But in JavaScript, a non-empty string is always `true`. |
| 111 | +Alte limbaje (anume PHP) trateaza `"0"` ca și `false`. Dar in JavaScript, un șir ne-gol este întotdeauna `true`. |
111 | 112 |
|
112 | 113 | ```js run |
113 | 114 | alert( Boolean("0") ); // true |
114 | | -alert( Boolean(" ") ); // spaces, also true (any non-empty string is true) |
| 115 | +alert( Boolean(" ") ); // spații, de asemenea true (orice șir ne-gol este true) |
115 | 116 | ``` |
116 | 117 | ```` |
117 | 118 |
|
118 | 119 | ## Summary |
| 120 | +## Rezumat |
119 | 121 |
|
120 | 122 | The three most widely used type conversions are to string, to number, and to boolean. |
121 | 123 |
|
|
0 commit comments