การแปลงประเภท 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
วิธีการจำนวน
ในบทNumber Methodsคุณจะพบวิธีการเพิ่มเติมที่สามารถใช้เพื่อแปลงสตริงเป็นตัวเลข:
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 |
The Unary + Operator
ตัวดำเนินการunary +สามารถใช้เพื่อแปลงตัวแปรเป็นตัวเลข:
ตัวอย่าง
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()
วิธีการเพิ่มเติม
ในบทNumber Methodsคุณจะพบวิธีการอื่นๆ ที่สามารถใช้เพื่อแปลงตัวเลขเป็นสตริงได้:
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)"
วิธีวันที่toString()
ทำเช่นเดียวกัน
ตัวอย่าง
Date().toString() // returns "Thu Jul 17 2014 15:38:19 GMT+0200 (W. Europe Daylight Time)"
ในบทDate Methodsคุณจะพบวิธีการเพิ่มเติมที่สามารถใช้เพื่อแปลงวันที่เป็นสตริง:
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 พยายามดำเนินการกับประเภทข้อมูลที่ "ผิด" 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
การแปลงสตริงอัตโนมัติ
JavaScript เรียกใช้ฟังก์ชันของตัวแปรโดยอัตโนมัติtoString()
เมื่อคุณพยายาม "ส่งออก" วัตถุหรือตัวแปร:
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 ต่างๆ เป็น Number, String และ 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 |
ค่าในเครื่องหมายคำพูดระบุค่าสตริง
ค่าสีแดงแสดงถึงค่า (บางส่วน) ที่โปรแกรมเมอร์อาจคาดไม่ถึง