JavaScript Türü Dönüştürme
- Dizeleri Sayılara Dönüştürme
- Sayıları Dizelere Dönüştürme
- Tarihleri Sayılara Dönüştürme
- Sayıları Tarihlere Dönüştürme
- Booleanları Sayılara Dönüştürme
- Sayıları Booleanlara Dönüştürme
JavaScript Türü Dönüştürme
JavaScript değişkenleri yeni bir değişkene ve başka bir veri türüne dönüştürülebilir:
- JavaScript işlevinin kullanımıyla
- JavaScript'in kendisi tarafından otomatik olarak
Dizeleri Sayılara Dönüştürme
Global yöntem Number()
, dizeleri sayılara dönüştürebilir.
Sayı içeren dizeler ("3.14" gibi) sayılara (3.14 gibi) dönüşür.
Boş dizeler 0'a dönüştürülür.
Başka herhangi bir şey NaN
(Sayı Değil) biçimine dönüşür.
Number("3.14") // returns 3.14
Number(" ") // returns 0
Number("") // returns 0
Number("99 88") // returns NaN
Sayı Yöntemleri
Sayı Yöntemleri bölümünde , dizeleri sayılara dönüştürmek için kullanılabilecek daha fazla yöntem bulacaksınız:
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 + Operatör
Bir değişkeni sayıya dönüştürmek için unary + operatörü kullanılabilir:
Örnek
let y = "5";
// y is a string
let x = + y;
// x is a number
Değişken dönüştürülemezse, yine bir sayı olur, ancak değeri NaN
(Sayı Değil):
Örnek
let y = "John";
// y is a string
let x = + y; // x is a number (NaN)
Sayıları Dizelere Dönüştürme
Global yöntem String()
, sayıları dizelere dönüştürebilir.
Her tür sayı, değişmez değer, değişken veya ifade üzerinde kullanılabilir:
Örnek
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 yöntemi toString()
de aynı şeyi yapar.
Örnek
x.toString()
(123).toString()
(100 + 23).toString()
Daha Fazla Yöntem
Sayı Yöntemleri bölümünde , sayıları dizgelere dönüştürmek için kullanılabilecek daha fazla yöntem bulacaksınız:
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 |
Tarihleri Sayılara Dönüştürme
Global yöntem Number()
, tarihleri sayılara dönüştürmek için kullanılabilir.
d = new Date();
Number(d) // returns 1404568027739
Tarih yöntemi getTime()
de aynı şeyi yapar.
d = new Date();
d.getTime() // returns 1404568027739
Tarihleri Dizelere Dönüştürme
Global yöntem String()
, tarihleri dizgelere dönüştürebilir.
String(Date()) // returns "Thu Jul 17 2014 15:38:19 GMT+0200 (W. Europe Daylight Time)"
Date yöntemi toString()
de aynı şeyi yapar.
Örnek
Date().toString() // returns "Thu Jul 17 2014 15:38:19 GMT+0200 (W. Europe Daylight Time)"
Tarih Yöntemleri bölümünde , tarihleri dizgelere dönüştürmek için kullanılabilecek daha fazla yöntem bulacaksınız:
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) |
Booleanları Sayılara Dönüştürme
Global yöntem Number()
ayrıca booleanları sayılara da dönüştürebilir.
Number(false) // returns 0
Number(true) // returns 1
Booleanları Dizelere Dönüştürmek
Global yöntem String()
, booleanları dizelere dönüştürebilir.
String(false) // returns "false"
String(true) // returns "true"
Boole yöntemi toString()
de aynı şeyi yapar.
false.toString() // returns "false"
true.toString() // returns "true"
Otomatik Tip Dönüşümü
JavaScript "yanlış" bir veri türü üzerinde çalışmaya çalıştığında, değeri "doğru" bir türe dönüştürmeye çalışacaktır.
Sonuç her zaman beklediğiniz gibi olmaz:
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
Otomatik Dize Dönüştürme
JavaScript, toString()
bir nesneyi veya değişkeni "çıktılamaya" çalıştığınızda değişkenin işlevini otomatik olarak çağırır:
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"
Sayılar ve booleler de dönüştürülür, ancak bu çok görünür değildir:
// if myVar = 123 // toString converts to "123"
// if myVar = true // toString converts to "true"
// if myVar = false // toString converts to "false"
JavaScript Türü Dönüşüm Tablosu
Bu tablo, farklı JavaScript değerlerinin Number, String ve Boolean'a dönüştürülmesinin sonucunu gösterir:
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 |
Tırnak içindeki değerler, dize değerlerini gösterir.
Kırmızı değerler , programcıların beklemeyebileceği (bazı) değerleri gösterir.