|
1 | | -# Type Conversions |
| 1 | +# Tipų konversijos |
2 | 2 |
|
3 | | -Most of the time, operators and functions automatically convert the values given to them to the right type. |
| 3 | +Dažniausiai operatoriai ir funkcijos automatiškai pakeičia jiems duotas vertes į teisingą tipą. |
4 | 4 |
|
5 | | -For example, `alert` automatically converts any value to a string to show it. Mathematical operations convert values to numbers. |
| 5 | +Pavyzdžiui `alert` automatiškai paverčia bet kokią jiems duotą vertę į eilutės tipą, kad galėtų jį parodytų. Matematinės operacijos pakeičia vertes į skaičius. |
6 | 6 |
|
7 | | -There are also cases when we need to explicitly convert a value to the expected type. |
| 7 | +Yra tokių konkrečių atvejų kai mums reikia vertę pakeisti į atitinkamą tipą. |
8 | 8 |
|
9 | | -```smart header="Not talking about objects yet" |
10 | | -In this chapter, we won't cover objects. Instead, we'll study primitives first. Later, after we learn about objects, we'll see how object conversion works in the chapter <info:object-toprimitive>. |
| 9 | +```smart header="Dar nekalbame apie objektus" |
| 10 | +Šiame skyriuje kol kas dar nekalbėsime apie objektus. Vietoje to studijuosime primityvius tipus. Vėliau kai sužinosime daugiau apie objektus, pamatysime kaip objektų keitimai veikia skyriuje <info:object-toprimitive>. |
11 | 11 | ``` |
12 | 12 |
|
13 | | -## String Conversion |
| 13 | +## Eilutės konversijos |
14 | 14 |
|
15 | | -String conversion happens when we need the string form of a value. |
| 15 | +Eilutės keitimas įvyksta tada kai mums reikia eilutės formos vertės. |
16 | 16 |
|
17 | | -For example, `alert(value)` does it to show the value. |
| 17 | +Pavyzdžiui, `alert(value)` tai padaro, kad parodytų vertę. |
18 | 18 |
|
19 | | -We can also call the `String(value)` function to convert a value to a string: |
| 19 | +Mes taip pat galime iššaukti `String(value)` funkciją, kad konvertuotume vertę į eilutę: |
20 | 20 |
|
21 | 21 | ```js run |
22 | 22 | let value = true; |
23 | | -alert(typeof value); // boolean |
| 23 | +alert(typeof value); // boolean (loginis) |
24 | 24 |
|
25 | 25 | *!* |
26 | | -value = String(value); // now value is a string "true" |
| 26 | +value = String(value); // dabar vertė yra eilutė "true" |
27 | 27 | alert(typeof value); // string |
28 | 28 | */!* |
29 | 29 | ``` |
30 | 30 |
|
31 | | -String conversion is mostly obvious. A `false` becomes `"false"`, `null` becomes `"null"`, etc. |
| 31 | +Eilutės konversijos dažniausiai yra labai akivaizdžios. `false` tampa `"false"`, `null` tampa `"null"` ir t.t. |
32 | 32 |
|
33 | | -## Numeric Conversion |
| 33 | +## Skaičių konversijos |
34 | 34 |
|
35 | | -Numeric conversion happens in mathematical functions and expressions automatically. |
| 35 | +Skaičių konversijos įvyksta automatiškai matematinėse funkcijose ir formulėse. |
36 | 36 |
|
37 | | -For example, when division `/` is applied to non-numbers: |
| 37 | +Pavyzdžiui, kai dalyba `/` taikoma ne skaičiams: |
38 | 38 |
|
39 | 39 | ```js run |
40 | | -alert( "6" / "2" ); // 3, strings are converted to numbers |
| 40 | +alert( "6" / "2" ); // 3, eilutės paverčiamos skaičiais |
41 | 41 | ``` |
42 | 42 |
|
43 | | -We can use the `Number(value)` function to explicitly convert a `value` to a number: |
| 43 | +Mes taip pat galime naudoti `Number(value)` funkciją konkrečiai tam, kad paverstume `value` į skaičių: |
44 | 44 |
|
45 | 45 | ```js run |
46 | 46 | let str = "123"; |
47 | 47 | alert(typeof str); // string |
48 | 48 |
|
49 | | -let num = Number(str); // becomes a number 123 |
| 49 | +let num = Number(str); // tampa numeriu 123 |
50 | 50 |
|
51 | 51 | alert(typeof num); // number |
52 | 52 | ``` |
53 | 53 |
|
54 | | -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. |
| 54 | +Akivaizdžios konversijos dažniausiai reikalingos kai gauname vertę eilutės tipo formatu iš tekstinių šaltinių, bet mums iš tikrųjų reikalingas skaičius. |
55 | 55 |
|
56 | | -If the string is not a valid number, the result of such a conversion is `NaN`. For instance: |
| 56 | +Jeigu eilutė nėra tinkamas skaičius, tada tokios konversijos rezultatas bus is `NaN`. Pavyzdžiui: |
57 | 57 |
|
58 | 58 | ```js run |
59 | | -let age = Number("an arbitrary string instead of a number"); |
| 59 | +let age = Number("arbitriška eilutė vietoje skaičiaus"); |
60 | 60 |
|
61 | | -alert(age); // NaN, conversion failed |
| 61 | +alert(age); // NaN, konversija nepavyko |
62 | 62 | ``` |
63 | 63 |
|
64 | | -Numeric conversion rules: |
| 64 | +Skaičių konversijos taisyklės: |
65 | 65 |
|
66 | | -| Value | Becomes... | |
| 66 | +| Vertė | Tampa... | |
67 | 67 | |-------|-------------| |
68 | 68 | |`undefined`|`NaN`| |
69 | 69 | |`null`|`0`| |
70 | | -|<code>true and false</code> | `1` and `0` | |
71 | | -| `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`. | |
| 70 | +|<code>true ir false</code> | `1` ir `0` | |
| 71 | +| `string` | Tarpai pradžioje ir pabaigoje panaikinami. Jeigu likusi eilutė yra tuščia, rezultatas yra `0`. Kitu atveju, skaičius "perskaitomas" iš eilutės. Klaida grąžina `NaN`. | |
72 | 72 |
|
73 | | -Examples: |
| 73 | +Pavyzdžiai: |
74 | 74 |
|
75 | 75 | ```js run |
76 | 76 | alert( Number(" 123 ") ); // 123 |
77 | | -alert( Number("123z") ); // NaN (error reading a number at "z") |
| 77 | +alert( Number("123z") ); // NaN (klaida perskaitė skaičiuje "z") |
78 | 78 | alert( Number(true) ); // 1 |
79 | 79 | alert( Number(false) ); // 0 |
80 | 80 | ``` |
81 | 81 |
|
82 | | -Please note that `null` and `undefined` behave differently here: `null` becomes zero while `undefined` becomes `NaN`. |
| 82 | +Atkreipkite dėmesį, kad `null` ir `undefined` elgiasi kitaip šiuo atveju: `null` tampa nuliu kai `undefined` tampa `NaN`. |
83 | 83 |
|
84 | | -````smart header="Addition '+' concatenates strings" |
85 | | -Almost all mathematical operations convert values to numbers. A notable exception is addition `+`. If one of the added values is a string, the other one is also converted to a string. |
| 84 | +````smart header="Sudėtis '+' sujungia eilutes" |
| 85 | +Beveik visos matematinės operacijos paverčia vertes numeriais. A notable exception is addition `+`. If one of the added values is a string, the other one is also converted to a string. |
86 | 86 |
|
87 | 87 | Then, it concatenates (joins) them: |
88 | 88 |
|
|
0 commit comments