Konverze typu JavaScript
- Převod řetězců na čísla
- Převod čísel na řetězce
- Převod dat na čísla
- Převod čísel na data
- Převod booleovských hodnot na čísla
- Převod čísel na booleovské
Konverze typu JavaScript
Proměnné JavaScriptu lze převést na novou proměnnou a jiný datový typ:
- Pomocí funkce JavaScript
- Automaticky samotným JavaScriptem
Převod řetězců na čísla
Globální metoda Number()
může převádět řetězce na čísla.
Řetězce obsahující čísla (jako "3.14") se převedou na čísla (jako 3.14).
Prázdné řetězce se převedou na 0.
Cokoli jiného se převede na NaN
(ne na číslo).
Number("3.14") // returns 3.14
Number(" ") // returns 0
Number("") // returns 0
Number("99 88") // returns NaN
Metody čísel
V kapitole Metody čísel najdete další metody, které lze použít k převodu řetězců na čísla:
Method | Description |
---|---|
Number() | Returns a number, converted from its argument |
parseFloat() | Parses a string and returns a floating point number |
parseInt() | Parses a string and returns an integer |
Unary + operátor
Unární operátor + lze použít k převodu proměnné na číslo:
Příklad
let y = "5";
// y is a string
let x = + y;
// x is a number
Pokud proměnnou nelze převést, stane se stále číslem, ale s hodnotou NaN
(není číslem):
Příklad
let y = "John";
// y is a string
let x = + y; // x is a number (NaN)
Převod čísel na řetězce
Globální metoda String()
může převádět čísla na řetězce.
Lze jej použít na jakýkoli typ čísel, literálů, proměnných nebo výrazů:
Příklad
String(x) // returns a string from a number variable x
String(123) // returns a string from a number literal 123
String(100 + 23) // returns a string from a number from an expression
toString()
Totéž dělá metoda Number .
Příklad
x.toString()
(123).toString()
(100 + 23).toString()
Více metod
V kapitole Metody čísel najdete další metody, které lze použít k převodu čísel na řetězce:
Method | Description |
---|---|
toExponential() | Returns a string, with a number rounded and written using exponential notation. |
toFixed() | Returns a string, with a number rounded and written with a specified number of decimals. |
toPrecision() | Returns a string, with a number written with a specified length |
Převod dat na čísla
Number()
K převodu dat na čísla lze použít globální metodu .
d = new Date();
Number(d) // returns 1404568027739
Metoda data getTime()
dělá totéž.
d = new Date();
d.getTime() // returns 1404568027739
Převod dat na řetězce
Globální metoda String()
může převádět data na řetězce.
String(Date()) // returns "Thu Jul 17 2014 15:38:19 GMT+0200 (W. Europe Daylight Time)"
toString()
Totéž dělá metoda Date .
Příklad
Date().toString() // returns "Thu Jul 17 2014 15:38:19 GMT+0200 (W. Europe Daylight Time)"
V kapitole Metody data najdete další metody, které lze použít k převodu dat na řetězce:
Method | Description |
---|---|
getDate() | Get the day as a number (1-31) |
getDay() | Get the weekday a number (0-6) |
getFullYear() | Get the four digit year (yyyy) |
getHours() | Get the hour (0-23) |
getMilliseconds() | Get the milliseconds (0-999) |
getMinutes() | Get the minutes (0-59) |
getMonth() | Get the month (0-11) |
getSeconds() | Get the seconds (0-59) |
getTime() | Get the time (milliseconds since January 1, 1970) |
Převod booleovských hodnot na čísla
Globální metoda Number()
může také převádět booleany na čísla.
Number(false) // returns 0
Number(true) // returns 1
Převod booleovských hodnot na řetězce
Globální metoda String()
dokáže převést booleovské hodnoty na řetězce.
String(false) // returns "false"
String(true) // returns "true"
Booleovská metoda toString()
dělá totéž.
false.toString() // returns "false"
true.toString() // returns "true"
Automatická konverze typu
Když se JavaScript pokusí pracovat na „nesprávném“ datovém typu, pokusí se převést hodnotu na „správný“ typ.
Výsledek není vždy takový, jaký očekáváte:
5 + null // returns 5 because null is converted to 0
"5" + null // returns "5null" because null is converted to "null"
"5" + 2 // returns
"52" because 2 is converted to "2"
"5" - 2 // returns 3 because "5" is converted to 5
"5" * "2" // returns
10 because "5" and "2" are
converted to 5 and 2
Automatická konverze řetězců
JavaScript automaticky volá funkci proměnné, toString()
když se pokusíte „vypsat“ objekt nebo proměnnou:
document.getElementById("demo").innerHTML = myVar;
// if myVar = {name:"Fjohn"} // toString converts to "[object Object]"
// if myVar = [1,2,3,4] // toString converts to "1,2,3,4"
// if myVar = new Date() // toString converts to "Fri Jul 18 2014 09:08:55 GMT+0200"
Čísla a booleovské hodnoty jsou také převedeny, ale není to příliš viditelné:
// if myVar = 123 // toString converts to "123"
// if myVar = true // toString converts to "true"
// if myVar = false // toString converts to "false"
Konverzní tabulka typů JavaScript
Tato tabulka ukazuje výsledek převodu různých hodnot JavaScriptu na Number, String a Boolean:
Original Value |
Converted to Number |
Converted to String |
Converted to Boolean |
Try it |
---|---|---|---|---|
false | 0 | "false" | false | |
true | 1 | "true" | true | |
0 | 0 | "0" | false | |
1 | 1 | "1" | true | |
"0" | 0 | "0" | true | |
"000" | 0 | "000" | true | |
"1" | 1 | "1" | true | |
NaN | NaN | "NaN" | false | |
Infinity | Infinity | "Infinity" | true | |
-Infinity | -Infinity | "-Infinity" | true | |
"" | 0 | "" | false | |
"20" | 20 | "20" | true | |
"twenty" | NaN | "twenty" | true | |
[ ] | 0 | "" | true | |
[20] | 20 | "20" | true | |
[10,20] | NaN | "10,20" | true | |
["twenty"] | NaN | "twenty" | true | |
["ten","twenty"] | NaN | "ten,twenty" | true | |
function(){} | NaN | "function(){}" | true | |
{ } | NaN | "[object Object]" | true | |
null | 0 | "null" | false | |
undefined | NaN | "undefined" | false |
Hodnoty v uvozovkách označují hodnoty řetězce.
Červené hodnoty označují hodnoty, které (někteří) programátoři neočekávají.