解釋 JavaScript 中的型別轉換?
JavaScript 中的型別轉換是指將一種資料型別轉換為另一種資料型別,例如將字串資料型別轉換為布林值或將整數資料型別轉換為字串資料型別。JavaScript 中的型別轉換也稱為型別轉換或型別強制。
轉換型別
JavaScript 中的型別轉換分為兩種:隱式型別轉換和顯式型別轉換。
隱式型別轉換
隱式型別轉換是由內部需求或編譯器或直譯器自動進行的資料型別轉換。
為了理解隱式轉換,讓我們考慮布林值(原始型別)的示例。
JavaScript 在條件表示式中需要布林值。因此,JavaScript 將臨時將括號中的值轉換為布林值以評估 if 表示式。
示例
val = 1; if (val) { console.log( 'yes, val exists' ); }
值 0、-0、''(空字串)、NaN、undefined 和 null 將評估為 false,所有其他值都評估為 true,即使是空陣列和物件。
使用 == 運算子進行隱式轉換
使用等於 (==) 和不等於 (!=) 運算子比較值時,也會執行型別轉換。因此,當使用等於 (==) 運算子將數字 125 與字串 '125' 進行比較時,表示式將評估為 true。
console.log( 125 == '125' );
使用全等 (===) 和不全等 (!==) 運算子時,不會執行型別轉換。
顯式型別轉換
第二種型別轉換,顯式型別轉換,是由開發者為了獲得良好的程式碼而強制執行的。在 JavaScript 中,型別轉換隻能對字串、數字和布林值(物件)資料型別進行。
顯式型別轉換的示例包括 parseInt()、parseFloat() 和 toString() 方法。
parseInt() 函式將其第一個引數轉換為字串,解析該字串,然後返回一個整數或 NaN。
parseFloat() 函式解析引數(如果需要,先將其轉換為字串),並返回一個浮點數。
toString() 方法返回表示物件的字串,即它嘗試將物件轉換為字串。
示例
以下示例演示了 JavaScript 中的顯式型別轉換。
let a = 1.015 console.log(a) console.log(typeof a) console.log(a.toString()) console.log(typeof a.toString())
字串中的型別轉換
在 JavaScript 中,字串被視為物件。因此,在字串型別轉換中,任何數字或字元都將轉換為字串。String() 用於將任何給定值轉換為字串。
語法
字串的型別轉換可以使用以下語法完成。
String(input)
String() 方法將接受一個引數,即要轉換為字串的輸入。
示例
以下是此函式的示例:
input = 25 console.log("The input value with its type is:",input,typeof(input)) console.log("Arithmetic operation before typecasting with its type is:", (10 + input), typeof((10 + input))) sInput = String(input) console.log("After the type casting is done the type is:",sInput,typeof(sInput)) console.log("Arithmetic operation after typecasting with its type is:", (10 + sInput), typeof(10 + sInput))
在上面的示例中,給定的輸入是一個數字,在進行型別轉換之前,它會對數字進行加法運算。但是,當給定的輸入被 String() 方法轉換為字串時,它不會與 10 相加,而是被連線起來,因為即使另一個運算元是數字,其中一個運算元也是字串。這表明型別轉換已經完成。
布林值中的型別轉換
要將給定的輸入轉換為布林值,可以使用 Boolean(),它將給定的值作為引數並將其轉換為布林值形式。此方法將根據情況返回布林值“true”或“false”。如果輸入至少是一個字元、任何非零數字或物件,則返回 true。如果給定的輸入是空字串、零、undefined 或 null 值,則返回 false。
示例
以下是 JavaScript 中使用 Boolean() 進行布林值型別轉換的示例:
console.log("The cases where the method will return true") console.log("The given input is rw",Boolean('rw')) console.log("The given input is -3.6",Boolean(-3.6)) console.log("The given input is new Date()",Boolean(new Date())) console.log("The cases where the method will return false") console.log("The given input is number 0",Boolean(0) ) console.log("The given input is null value",Boolean(null) ) console.log("The given input is undefined",Boolean(undefined)) console.log("The given input is empty string",Boolean('') )
數值型別轉換
要將給定的輸入轉換為數字,可以使用 Number() 方法。它接受一個引數,即要轉換為數字的輸入。轉換可以轉換為浮點型別或整數型別。
要將其轉換為整數型別,可以使用 parseInt() 方法。要將其轉換為浮點型別,可以使用 parseFloat() 方法。
語法
Number(input) parseInt(input) parseFloat(input)
這三種方法都將給定的輸入作為引數,並根據使用的方法進行相應的轉換。
示例
此示例演示了 JavaScript 中的 Number() 型別轉換:
console.log("Input is 7.8.9 and after conversion",Number("7.8.9")) console.log("Input is 6.6.6 and after conversion to float is",parseInt("6.6.6")) console.log("Input is 6.6.6 and after conversion to float is",parseFloat("6.6.6"))
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP