Dalam tutorial ini, anda akan belajar tentang penukaran jenis dalam JavaScript dengan bantuan contoh.
Proses menukar satu jenis data ke jenis data yang lain disebut penukaran jenis. Terdapat dua jenis penukaran jenis dalam JavaScript.
- Penukaran Tersirat
- Penukaran Eksplisit
Penukaran Tersirat JavaScript
Dalam situasi tertentu, JavaScript secara automatik menukar satu jenis data ke jenis yang lain (ke jenis yang betul). Ini dikenali sebagai penukaran tersirat.
Contoh 1: Penukaran Tersirat ke Rentetan
// numeric string used with + gives string type let result; result = '3' + 2; console.log(result) // "32" result = '3' + true; console.log(result); // "3true" result = '3' + undefined; console.log(result); // "3undefined" result = '3' + null; console.log(result); // "3null"
Catatan : Apabila nombor ditambahkan ke rentetan, JavaScript mengubah nombor menjadi rentetan sebelum penggabungan.
Contoh 2: Penukaran Tersirat ke Nombor
// numeric string used with - , / , * results number type let result; result = '4' - '2'; console.log(result); // 2 result = '4' - 2; console.log(result); // 2 result = '4' * 2; console.log(result); // 8 result = '4' / 2; console.log(result); // 2
Contoh 3: Hasil Rentetan Bukan Nombor ke NaN
// non-numeric string used with +, - , / , * results to NaN let result; result = 'hello' - 'world'; console.log(result); // NaN result = '4' - 'hello'; console.log(result); // NaN
Contoh 4: Penukaran Boolean Tersirat ke Nombor
// if boolean is used, true is 1, false is 0 let result; result = '4' - true; console.log(result); // 3 result = 4 + true; console.log(result); // 5 result = 4 + false; console.log(result); // 4
Catatan: JavaScript menganggap 0 sebagai false
dan semua nombor bukan sifar sebagai true
. Dan, jika true
ditukar menjadi nombor, hasilnya selalu 1.
Contoh 5: Penukaran nol ke Nombor
// null is 0 when used with number let result; result = 4 + null; console.log(result); // 4 result = 4 - null; console.log(result); // 4
Contoh 6: tidak ditentukan digunakan dengan nombor, boolean atau null
// Arithmetic operation of undefined with number, boolean or null gives NaN let result; result = 4 + undefined; console.log(result); // NaN result = 4 - undefined; console.log(result); // NaN result = true + undefined; console.log(result); // NaN result = null + undefined; console.log(result); // NaN
Penukaran Eksplisit JavaScript
Anda juga dapat mengubah satu jenis data ke yang lain sesuai dengan keperluan anda. Penukaran jenis yang anda lakukan secara manual dikenali sebagai penukaran jenis eksplisit.
Dalam JavaScript, penukaran jenis eksplisit dilakukan menggunakan kaedah terbina dalam.
Berikut adalah beberapa kaedah biasa untuk penukaran eksplisit.
1. Tukarkan ke Nombor Secara Eksplisit
Untuk menukar rentetan angka dan nilai boolean menjadi nombor, anda boleh menggunakan Number()
. Sebagai contoh,
let result; // string to number result = Number('324'); console.log(result); // 324 result = Number('324e-1') console.log(result); // 32.4 // boolean to number result = Number(true); console.log(result); // 1 result = Number(false); console.log(result); // 0
Dalam JavaScript, rentetan dan null
nilai kosong kembali 0 . Sebagai contoh,
let result; result = Number(null); console.log(result); // 0 let result = Number(' ') console.log(result); // 0
Sekiranya rentetan adalah nombor yang tidak sah, hasilnya akan menjadi NaN
. Sebagai contoh,
let result; result = Number('hello'); console.log(result); // NaN result = Number(undefined); console.log(result); // NaN result = Number(NaN); console.log(result); // NaN
Catatan : Anda juga boleh menjana nombor dari rentetan menggunakan parseInt()
, parseFloat()
operator yang tidak aktif +
dan Math.floor()
. Sebagai contoh,
let result; result = parseInt('20.01'); console.log(result); // 20 result = parseFloat('20.01'); console.log(result); // 20.01 result = +'20.01'; console.log(result); // 20.01 result = Math.floor('20.01'); console.log(result); // 20
2. Tukarkan ke Rentetan Secara Eksplisit
Untuk menukar jenis data lain ke rentetan, anda boleh menggunakan salah satu String()
atau toString()
. Sebagai contoh,
//number to string let result; result = String(324); console.log(result); // "324" result = String(2 + 4); console.log(result); // "6" //other data types to string result = String(null); console.log(result); // "null" result = String(undefined); console.log(result); // "undefined" result = String(NaN); console.log(result); // "NaN" result = String(true); console.log(result); // "true" result = String(false); console.log(result); // "false" // using toString() result = (324).toString(); console.log(result); // "324" result = true.toString(); console.log(result); // "true"
Nota : String()
mengambil null
dan undefined
dan saudara mereka untuk tali. Namun, toString()
memberikan kesalahan ketikanull are passed.
3. Tukar ke Boolean Secara Eksplisit
Untuk menukar jenis data lain menjadi boolean, anda boleh menggunakan Boolean().
Dalam JavaScript, undefined
, null
, 0
, NaN
, ''
memeluk false
. Sebagai contoh,
let result; result = Boolean(''); console.log(result); // false result = Boolean(0); console.log(result); // false result = Boolean(undefined); console.log(result); // false result = Boolean(null); console.log(result); // false result = Boolean(NaN); console.log(result); // false
Semua nilai lain memberi true
. Sebagai contoh,
result = Boolean(324); console.log(result); // true result = Boolean('hello'); console.log(result); // true result = Boolean(' '); console.log(result); // true
Jadual Penukaran Jenis JavaScript
Jadual menunjukkan penukaran nilai yang berbeza menjadi String, Number, dan Boolean dalam JavaScript.
Value | String Conversion | Number Conversion | Boolean Conversion |
1 | "1" | 1 |
|
0 | "0" | 0 |
|
"1" | "1" | 1 |
|
"0" | "0" | 0 |
|
"ten" | "ten" |
|
|
| "true" | 1 |
|
| "false" | 0 |
|
| "null" | 0 |
|
| "undefined" |
|
|
'' | "" | 0 |
|
' ' | " " | 0 |
|
Anda akan belajar mengenai penukaran objek dan tatasusunan ke jenis data lain dalam tutorial kemudian.
Lawati contoh-contoh ini untuk mengetahui lebih lanjut:
- Tarikh JavaScript hingga Nombor
- Tarikh JavaScript ke Rentetan