JavaScript型変換
- 文字列を数値に変換する
- 数値を文字列に変換する
- 日付を数値に変換する
- 数値を日付に変換する
- ブール値を数値に変換する
- 数値をブール値に変換する
JavaScript型変換
JavaScript変数は、新しい変数と別のデータ型に変換できます。
- JavaScript関数を使用する
- JavaScript自体によって自動的に
文字列を数値に変換する
グローバルメソッドNumber()
は、文字列を数値に変換できます。
数字を含む文字列(「3.14」など)は、数字(3.14など)に変換されます。
空の文字列は0に変換されます。
NaN
それ以外は(数値ではなく)に変換されます。
Number("3.14") // returns 3.14
Number(" ") // returns 0
Number("") // returns 0
Number("99 88") // returns NaN
数値メソッド
「数値メソッド」の章には、文字列を数値に変換するために使用できるその他のメソッドがあります。
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 |
単項+演算子
単項+演算子を使用して、変数を数値に変換できます。
例
let y = "5";
// y is a string
let x = + y;
// x is a number
変数を変換できない場合でも、変数は数値になりますが、値はNaN
(数値ではありません)になります。
例
let y = "John";
// y is a string
let x = + y; // x is a number (NaN)
数値を文字列に変換する
グローバルメソッドString()
は、数値を文字列に変換できます。
あらゆるタイプの数値、リテラル、変数、または式で使用できます。
例
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
NumberメソッドtoString()
も同じことをします。
例
x.toString()
(123).toString()
(100 + 23).toString()
その他のメソッド
「数値メソッド」の章には、数値を文字列に変換するために使用できるその他のメソッドがあります。
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 |
日付を数値に変換する
グローバルメソッドNumber()
を使用して、日付を数値に変換できます。
d = new Date();
Number(d) // returns 1404568027739
日付メソッドgetTime()
も同じです。
d = new Date();
d.getTime() // returns 1404568027739
日付を文字列に変換する
グローバルメソッドString()
は、日付を文字列に変換できます。
String(Date()) // returns "Thu Jul 17 2014 15:38:19 GMT+0200 (W. Europe Daylight Time)"
DateメソッドtoString()
も同じことをします。
例
Date().toString() // returns "Thu Jul 17 2014 15:38:19 GMT+0200 (W. Europe Daylight Time)"
「日付メソッド」の章には、日付を文字列に変換するために使用できるその他のメソッドがあります。
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) |
ブール値を数値に変換する
グローバルメソッドNumber()
は、ブール値を数値に変換することもできます。
Number(false) // returns 0
Number(true) // returns 1
ブール値から文字列への変換
グローバルメソッドString()
はブール値を文字列に変換できます。
String(false) // returns "false"
String(true) // returns "true"
ブール法toString()
も同じことをします。
false.toString() // returns "false"
true.toString() // returns "true"
自動型変換
JavaScriptが「間違った」データ型を操作しようとすると、値を「正しい」型に変換しようとします。
結果は必ずしもあなたが期待するものではありません:
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
自動文字列変換
toString()
オブジェクトまたは変数を「出力」しようとすると、JavaScriptは変数の関数を自動的に呼び出します。
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"
数値とブール値も変換されますが、これはあまり目立ちません。
// if myVar = 123 // toString converts to "123"
// if myVar = true // toString converts to "true"
// if myVar = false // toString converts to "false"
JavaScript型変換テーブル
この表は、さまざまなJavaScript値を数値、文字列、およびブール値に変換した結果を示しています。
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 |
引用符で囲まれた値は、文字列値を示します。
赤い値は、プログラマーが予期しない値(一部)を示します。