JavaScript အမျိုးအစားပြောင်းခြင်း။
- ကြိုးများကို နံပါတ်များအဖြစ် ပြောင်းလဲခြင်း။
- နံပါတ်များကို စာတန်းများအဖြစ် ပြောင်းလဲခြင်း။
- ရက်စွဲများကို နံပါတ်များအဖြစ် ပြောင်းလဲခြင်း။
- နံပါတ်များကို ရက်စွဲများအဖြစ် ပြောင်းလဲခြင်း။
- Booleans ကို နံပါတ်များအဖြစ် ပြောင်းလဲခြင်း။
- နံပါတ်များကို Booleans အဖြစ်ပြောင်းလဲခြင်း။
JavaScript အမျိုးအစားပြောင်းခြင်း။
JavaScript variable များကို variable အသစ်တစ်ခုနှင့် အခြား data type သို့ ပြောင်းနိုင်သည်-
- JavaScript function ကိုအသုံးပြုခြင်းအားဖြင့်
- 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 |
Unary + အော်ပရေတာ
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
နံပါတ်နည်းလမ်း 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 method 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) |
Booleans ကို နံပါတ်များအဖြစ် ပြောင်းလဲခြင်း။
global method Number()
သည် boolean ကို နံပါတ်များသို့လည်း ပြောင်းပေးနိုင်သည်။
Number(false) // returns 0
Number(true) // returns 1
Booleans အား ကြိုးများအဖြစ် ပြောင်းလဲခြင်း။
global method String()
သည် boolean များကို strings များအဖြစ် ပြောင်းလဲနိုင်သည်။
String(false) // returns "false"
String(true) // returns "true"
Boolean Method 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 တန်ဖိုးများကို 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 |
ကိုးကားရှိတန်ဖိုးများသည် စာကြောင်းတန်ဖိုးများကိုဖော်ပြသည်။
အနီရောင်တန်ဖိုး များသည် တန်ဖိုးများ (အချို့) ပရိုဂရမ်မာများ မျှော်လင့်မထားသော တန်ဖိုးများကို ဖော်ပြသည်။