I den här handledningen lär du dig om typkonverteringar i JavaScript med hjälp av exempel.
Processen att konvertera en datatyp till en annan datatyp kallas typkonvertering. Det finns två typer av typkonvertering i JavaScript.
- Implicit konvertering
- Explicit omvandling
JavaScript implicit konvertering
I vissa situationer konverterar JavaScript automatiskt en datatyp till en annan (till rätt typ). Detta kallas implicit konvertering.
Exempel 1: Implicit konvertering till sträng
// 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"
Obs! När ett nummer läggs till i en sträng konverterar JavaScript numret till en sträng före sammankoppling.
Exempel 2: Implicit konvertering till nummer
// 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
Exempel 3: Icke-numeriska strängresultat till 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
Exempel 4: Implicit Boolean Conversion to Number
// 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
Obs! JavaScript betraktar 0 som false
och alla tal som inte är noll som true
. Och om true
omvandlas till ett tal är resultatet alltid 1.
Exempel 5: null konvertering till nummer
// null is 0 when used with number let result; result = 4 + null; console.log(result); // 4 result = 4 - null; console.log(result); // 4
Exempel 6: odefinierad används med nummer, boolean eller 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
JavaScript Explicit Conversion
Du kan också konvertera en datatyp till en annan enligt dina behov. Den typkonvertering som du gör manuellt kallas uttrycklig typkonvertering.
I JavaScript görs konverteringar av exakt typ med hjälp av inbyggda metoder.
Här är några vanliga metoder för uttryckliga omvandlingar.
1. Konvertera till nummer uttryckligen
För att konvertera numeriska strängar och booleska värden till siffror kan du använda Number()
. Till exempel,
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
I JavaScript null
returnerar tomma strängar och värden 0 . Till exempel,
let result; result = Number(null); console.log(result); // 0 let result = Number(' ') console.log(result); // 0
Om en sträng är ett ogiltigt nummer blir resultatet NaN
. Till exempel,
let result; result = Number('hello'); console.log(result); // NaN result = Number(undefined); console.log(result); // NaN result = Number(NaN); console.log(result); // NaN
Obs : Du kan också generera tal från strängar med hjälp av parseInt()
, parseFloat()
, unär operator +
och Math.floor()
. Till exempel,
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. Konvertera till sträng uttryckligen
För att konvertera andra datatyper till strängar kan du använda antingen String()
eller toString()
. Till exempel,
//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"
Obs : String()
tar null
och undefined
konverterar dem till sträng. Ger dock toString()
fel närnull are passed.
3. Konvertera till Boolean uttryckligen
För att konvertera andra datatyper till en boolean kan du använda Boolean().
I JavaScript undefined
, null
, 0
, NaN
, ''
konverterar till false
. Till exempel,
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
Alla andra värden ger true
. Till exempel,
result = Boolean(324); console.log(result); // true result = Boolean('hello'); console.log(result); // true result = Boolean(' '); console.log(result); // true
JavaScript-omvandlingstabell
Tabellen visar omvandlingen av olika värden till Sträng, Antal och Boolean i 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 |
|
Du kommer att lära dig om konvertering av objekt och matriser till andra datatyper i senare självstudier.
Besök dessa exempel för att lära dig mer:
- JavaScript daterar till siffror
- JavaScript daterar till strängar