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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1.The addition with a string`"" + 1`converts `1`to a string: `"" + 1 = "1"`,and then we have `"1" + 0`,the same rule is applied.
2.The subtraction`-` (like most math operations) only works with numbers, it converts an empty string`""`to `0`.
3.The addition with a string appends the number`5`to the string.
4.The subtraction always converts to numbers, so it makes`" -9 "`a number `-9` (ignoring spaces around it).
5. `null`becomes `0`after the numeric conversion.
6. `undefined`becomes `NaN`after the numeric conversion.
7.Space characters, are trimmed off string start and end when a string is converted to a number. Here the whole string consists of space characters, such as`\t`, `\n`and a "regular" space between them. So, similarly to an empty string, it becomes `0`.
1.Sudėtis su eilute`"" + 1`paverčia `1`į eilutę: `"" + 1 = "1"`,o tada mes turime `"1" + 0`,taikoma ta pati taisyklė.
2.Atimtis`-` (kaip ir didžioji dalis matematinių operacijų) veikia tik su skaičiais, tad tuščią eilutę`""`paverčia į `0`.
3.Sudėtis su eilute prijungia skaičių`5`prie eilutės.
4.Atimtis visada paverčia į numerius, tad`" -9 "`tampa numeriu `-9` (ignoruoja tarpus aplink).
5. `null`tampa `0`po skaičių konversijos.
6. `undefined`tampa `NaN`po skaičių konversijos.
7.Tarpų ženklai yra nukerpami nuo eilutės pradžios ir pabaigos kai eilutė paverčiama į skaičių. Čia visa eilutė susideda iš tarpo ženklų kaip`\t`, `\n`ir "įprastinių" tarpų esančių tarp jų. Tad panašiai kaip ir tuščia eilutė, ji tampa `0`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Most of the time, operators and functions automatically convert the values given to them to the right type.
Dažniausiai operatoriai ir funkcijos automatiškai pakeičia jiems duotas vertes į teisingą tipą.
For example,`alert`automatically converts any value to a string to show it. Mathematical operations convert values to numbers.
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.
There are also cases when we need to explicitly convert a value to the expected type.
Yra tokių konkrečių atvejų kai mums reikia vertę pakeisti į atitinkamą tipą.
```smart header="Not talking about objects yet"
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>.
```smart header="Dar nekalbame apie objektus"
Š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>.
```
##String Conversion
##Eilutės konversijos
String conversion happens when we need the string form of a value.
Eilutės keitimas įvyksta tada kai mums reikia eilutės formos vertės.
For example, `alert(value)`does it to show the value.
Pavyzdžiui, `alert(value)`tai padaro, kad parodytų vertę.
We can also call the `String(value)`function to convert a value to a string:
Mes taip pat galime iššaukti `String(value)`funkciją, kad konvertuotume vertę į eilutę:
```js run
let value = true;
alert(typeof value); // boolean
alert(typeof value); // boolean (loginis)
*!*
value = String(value); //now value is a string "true"
value = String(value); //dabar vertė yra eilutė "true"
alert(typeof value); // string
*/!*
```
String conversion is mostly obvious. A`false`becomes `"false"`, `null`becomes `"null"`, etc.
Eilutės konversijos dažniausiai yra labai akivaizdžios.`false`tampa `"false"`, `null`tampa `"null"` ir t.t.
##Numeric Conversion
##Skaičių konversijos
Numeric conversion happens in mathematical functions and expressions automatically.
Skaičių konversijos įvyksta automatiškai matematinėse funkcijose ir formulėse.
For example, when division `/`is applied to non-numbers:
Pavyzdžiui, kai dalyba `/`taikoma ne skaičiams:
```js run
alert( "6" / "2" ); // 3,strings are converted to numbers
We can use the`Number(value)`function to explicitly convert a `value`to a number:
Mes taip pat galime naudoti`Number(value)`funkciją konkrečiai tam, kad paverstume `value`į skaičių:
```js run
let str = "123";
alert(typeof str); // string
let num = Number(str); //becomes a number 123
let num = Number(str); //tampa numeriu 123
alert(typeof num); // number
```
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.
Akivaizdžios konversijos dažniausiai reikalingos kai gauname vertę eilutės tipo formatu iš tekstinių šaltinių, bet mums iš tikrųjų reikalingas skaičius.
If the string is not a valid number, the result of such a conversionis `NaN`.For instance:
Jeigu eilutė nėra tinkamas skaičius, tada tokios konversijos rezultatas busis `NaN`.Pavyzdžiui:
```js run
let age = Number("an arbitrary string instead of a number");
let age = Number("arbitriška eilutė vietoje skaičiaus");
| `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`. |
| `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`. |
Examples:
Pavyzdžiai:
```js run
alert( Number(" 123 ") ); // 123
alert( Number("123z") ); // NaN (error reading a number at "z")
alert( Number("123z") ); // NaN (klaida perskaitė skaičiuje "z")
alert( Number(true) ); // 1
alert( Number(false) ); // 0
```
Please note that `null`and `undefined`behave differently here: `null`becomes zero while `undefined`becomes `NaN`.
Atkreipkite dėmesį, kad `null`ir `undefined`elgiasi kitaip šiuo atveju: `null`tampa nuliu kai `undefined`tampa `NaN`.
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.
````smart header="Sudėtis '+'sujungia eilutes"
Beveik visos matematinės operacijos paverčia vertes numeriais. Svarbi išimtis yra sudėtis`+`.Jeigu viena iš verčių yra eilutės, kita taip pat paverčiama eilute.
Then, it concatenates (joins) them:
Tada ji sujungia (ang. concatenates) vertes:
```js run
alert( 1 + '2' ); // '12' (string to the right)
alert( '1' + 2 ); // '12' (string to the left)
alert( 1 + '2' ); // '12' (eilutė iš dešinės)
alert( '1' + 2 ); // '12' (eilutė iš kairės)
```
This only happens when at least one of the arguments is a string. Otherwise, values are converted to numbers.
Taip įvyksta tik tokiu atveju kai vienas iš argumentų yra eilutė. Kitu atveju vertės konvertuojamos į skaičius.
````
##Boolean Conversion
##Loginės konversijos
Boolean conversion is the simplest one.
Loginės konversijos yra pačios paprasčiausios.
It happens in logical operations (later we'll meetcondition tests and other similar things) but can also be performed explicitly with a call to`Boolean(value)`.
Jos nutinka loginėse operacijose (vėliau dar matysime padėties testus, ang.condition tests, ir panašius atvejus), bet taip pat gali būti atliekamos akivaizdžiam`Boolean(value)` iškvietimui.
The conversion rule:
Konversijos taisyklės:
-Values that are intuitively "empty", like `0`,an empty string, `null`, `undefined`, and `NaN`,become `false`.
-Other values become `true`.
-Vertės kurios intuityviai yra tuščios, kaip `0`,tuščia eilutė, `null`, `undefined` ir `NaN`,tampa `false`.
````warn header="Please note: the string with zero`\"0\"`is `true`"
Some languages (namely PHP)treat`"0"`as `false`.But inJavaScript, a non-empty string is always `true`.
````warn header="Atkreipkite dėmesė: eilutė su nuliu`\"0\"`yra `true`"
Kai kurios kalbos (pavyzdžiui PHP) `"0"`laiko `false`.BetJavaScript netuščia eilutė visada bus `true`.
```js run
alert( Boolean("0") ); // true
alert( Boolean(" ") ); //spaces, also true (any non-empty string is true)
alert( Boolean(" ") ); //tarpai, taip pat tinka (bet kokia netuščia eilutė yra tinkama - true)
```
````
##Summary
##Santrauka
The three most widely used type conversions are to string, to number, and to boolean.
Trys labiausiai naudojamos tipų konversijos yra į eilutę, skaičių ir loginiai.
**`String Conversion`** --Occurs when we output something. Can be performed with `String(value)`.The conversion to string is usually obvious for primitive values.
**`Eilutės Konversija`** --Nutinka kai mes kažką gauname. Gali būti atliekama su `String(value)`.Konversija į eilutę su primityviais tipais dažniausiai yra akivaizdi.
**`Numeric Conversion`** --Occurs in math operations. Can be performed with `Number(value)`.
**`Skaičių Konversija`** --Nutinka matematinėse operacijos. Gali būti atliekama su `Number(value)`.
The conversion follows the rules:
Konversija laikosi taisyklių:
|Value |Becomes... |
|Vertė |Tampa... |
|-------|-------------|
|`undefined`|`NaN`|
|`null`|`0`|
|<code>true / false</code> | `1 / 0` |
| `string` |The string is read "as is", whitespaces from both sides are ignored. An empty string becomes`0`.An error gives `NaN`. |
| `string` |Eilutė skaitoma taip kaip yra, tarpai iš abiejų pusių ignoruojami. Tuščia eilutė tampa`0`.Klaida grąžina `NaN`. |
**`Boolean Conversion`** --Occurs in logical operations. Can be performed with `Boolean(value)`.
**`Loginės Konversijos`** --Nutinka loginėse operacijose. Gali būti atliekama su `Boolean(value)`.
Follows the rules:
Laikosi taisyklių:
|Value |Becomes... |
|Vertė |Tampa... |
|-------|-------------|
|`0`, `null`, `undefined`, `NaN`, `""` |`false`|
|any other value| `true` |
|bet kokia kita vertė| `true` |
Most of these rules are easy to understand and memorize. The notable exceptions where people usually make mistakes are:
Didžiąją dalį šių taisyklių lengva suprasti ir prisiminti. Svarbios išimtys kur žmonės daro klaidas yra:
- `undefined`is`NaN`as a number, not `0`.
- `"0"`and space-only strings like`" "`are true as a boolean.
- `undefined`kaip skaičius yra`NaN`kaip skaičius, ne `0`.
- `"0"`ir eilutė tik su tarpu kaip`" "`yra tiesa loginėse vertėse.
Objects aren't covered here. We'll return to them later in the chapter<info:object-toprimitive> that is devoted exclusively to objects after we learn more basic things about JavaScript.
Objektai čia neaptariami. Prie jų sugrįšime vėliau skyriuje<info:object-toprimitive>, kuris yra skirtas išskirtinai objektams kai tik sužinosime daugiau apie JavaScript.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.