JavaScript类型转换用法详细指南

JavaScript是一种松散类型的语言, 大多数时候, 运算符会自动将值转换为正确的类型, 但是在某些情况下, 我们需要显式进行类型转换。
尽管JavaScript提供了许多将数据从一种类型转换为另一种类型的方法, 但是有两种最常见的数据转换:

  • 将值转换为字符串
  • 将值转换为数字
隐式转换:
JavaScript中有各种运算符和函数, 它们会自动将值转换为正确的类型, 例如JavaScript中的alert()函数接受任何值并将其转换为字符串。但是各种运算符都会产生类似" +"运算符的问题。
例子:
Input: "2" + "3" Output: "23" here + operator stands for string concatenation in this case. But "3" - "1" gives output 2 by using Implicit Conversion.

代码1:
此代码显示了JavaScript中的隐式类型转换。
< script> document.write( '("3" - "1") = ' + ( "3" - "1" ) + "< br> " ); document.write( '("3" - 1) = ' + ( "3" - 1) + "< br> " ); document.write( '("3" * "2") = ' + ( "3" * "2" ) + "< br> " ); document.write( '("3" % "2") = ' + ( "3" % "2" ) + "< br> " ); document.write( '("3" + null) = ' + ( "3" + null ) + "< br> " ); < /script>

输出如下:
("3" - "1") = 2 ("3" - 1) = 2 ("3" * "2") = 6 ("3" % "2") = 1 ("3" + null) = 3null

将值转换为字符串:
在JavaScript中,可以使用String()或toString()函数将值转换为字符串。
String()函数的语法:
String(value)

例子:
Input: var v = 1555; var s = String(v); Output: now s contains "1555".

toString()函数的语法:
variableName.toString(base)

例子:
Input: var v = 1555; var s = v.toString(); Output: now s contains "1555".

有关toString()函数的更多信息, 请查看本文
JavaScript | toString()函数.
代码2:
下面的代码将数字转换为字符串, 布尔值转换为字符串, 日期转换为字符串。
< script> // Number and date has been assigned // to variable v and d respectively var v = 123; var d = new Date( '1995-12-17T03:24:00' ); // Conversion of number to string document.write( " String(v) = " + String(v) + "< br> " ); // Conversion of number to string document.write( " String(v + 11) = " + String(v + 11) + "< br> " ); document.write( " String( 10 + 10) = " + String(10 + 10) + "< br> " ); // Conversion of boolean value to string document.write( " String(false) = " + String( false ) + "< br> " ); // Conversion of Date to string document.write( " String(d) = " + String(d) + "< br> " ); < /script>

输出如下:
String(v) = 123 String(v + 11) = 134 String( 10 + 10) = 20 String(false) = false String(d) = Sun Dec 17 1995 03:24:00 GMT+0530 (India Standard Time)

将值转换为数字:
我们可以在JavaScript中使用Number()函数将值转换为数字。它可以将任何数字文本和布尔值转换为数字。在非数字字符串的情况下,它将把它转换为NaN(不是数字)。
语法如下:
Number(valueToConvert)

例子:
Input: var s = "144"; var n = Number(s); Output: now n contain 144(Number).

代码3:
下面的代码将数字文本, 日期和布尔值转换为数字。
< script> // Number and date has been assigned // to variable v and d respectively var v = "144" ; var d = new Date( '1995-12-17T03:24:00' ); // Conversion of string to number document.write( " Number(v) = " + Number(v) + "< br> " ); //Conversion of boolean value to number document.write( " Number(false) = " + Number( false ) + "< br> " ); document.write( " Number(true) = " + Number( true ) + "< br> " ); // Conversion of date to number document.write( " Number(d) = " + Number(d) + "< br> " ); < /script>

输出如下:
Number(v) = 144 Number(false) = 0 Number(true) = 1 Number(d) = 819150840000

代码4:
如果字符串是非数字,则它将其转换为NaN,而包含空格或空字符串的字符串将转换为0。
< script> // Empty string assigned var v = "" ; // White space assigned var d = " " ; // Non-number string assigned var s = "lsbin" ; // Printing converted values of number document.write( " Number(v) = " + Number(v) + "< br> " ); document.write( " Number(d) = " + Number(d) + "< br> " ); document.write( " Number(s) = " + Number(s) + "< br> " ); < /script>

【JavaScript类型转换用法详细指南】输出如下:
Number(v) = 0 Number(d) = 0 Number(s) = NaN

    推荐阅读