JavaScript - 类型转换
JavaScript 类型转换
JavaScript 中的类型转换是指在 JavaScript 中将数据从一种数据类型自动或显式地转换为另一种数据类型的过程。这些转换对于 JavaScript 有效地执行操作和比较至关重要。JavaScript 变量可以包含任何数据类型的值,因为它是一种弱类型语言。
JavaScript 中有两种类型转换 -
- 隐式类型转换
- 显式类型转换
隐式类型转换也称为强制转换。
隐式类型转换
当 JavaScript 自动完成类型转换时,这被称为隐式类型转换。例如,当我们将 '+' 运算符与 string 和 number 操作数一起使用时,JavaScript 会将 number 转换为 string,并将其与字符串连接起来。
以下是一些隐式类型转换的示例。
转换为字符串(隐式转换)
在此示例中,我们使用 '+' 运算符将不同的值隐式转换为字符串数据类型。
"100" + 24; // 将 24 转换为字符串 '100' + false; // 将 false 布尔值转换为字符串 "100" + null; // 将 null 关键字转换为字符串
请注意,要使用"+"运算符将值转换为字符串,其中一个操作数必须是字符串。
让我们尝试下面的示例,并检查输出 -
<html>
<head>
<title>Implicit conversion to string </title>
</head>
<body>
<script>
document.write("100" + 24 + "<br/>");
document.write('100' + false + "<br/>");
document.write("100" + null+ "<br/>");
document.write("100" + undefined+ "<br/>");
</script>
</body>
</html>
转换为数字(隐式转换)
当您将包含数字的字符串值与除"+"运算符之外的算术运算符一起使用时,它会自动将操作数转换为数字并执行算术运算,您可以在下面的示例中看到。
布尔值也会转换为数字。
'100' / 50; // 将 '100' 转换为 100 '100' - '50'; // 将 '100' 和 '50' 转换为 100 和 50 '100' * true; // 将 true 转换为 1 '100' - false; // 将 false 转换为 0 'tp' / 50 // 将 'tp' 转换为 NaN
尝试以下示例并检查输出 -
<html>
<head>
<title> Implicit conversion to Number </title>
</head>
<body>
<script>
document.write(('100' / 50) + "<br>");
document.write(('100' - '50') + "<br>");
document.write(('100' * true) + "<br>");
document.write(('100' - false) + "<br>");
document.write(('tp' / 50) + "<br>");
</script>
</body>
</html>
转换为布尔值(隐式转换)
对任何变量使用 Nullish (!!) 运算符时,它会将其值隐式转换为 boolean 值。
num = !!0; // !0 = true, !!0 = false num = !!1; // !1 = false, !!1 = true str = !!""; // !"" = true, !!"" = false str = !!"Hello"; // !"Hello" = false, !!"Hello" = true
Null 到 Number(隐式转换)
在 JavaScript 中,null 表示空。因此,当我们将 null 用作算术运算符的操作数时,它会自动转换为 0。
let num = 100 + null; // 将 null 转换为 0 num = 100 * null; // 将 null 转换为 0
Undefined 与 Number 和 Boolean 的转换(隐式转换)
将 undefined 与"number"或"boolean"值一起使用时,输出始终为 NaN。此处,NaN 表示非数字。
<html>
<head>
<title> Using undefined with a number and boolean value </title>
</head>
<body>
<script>
let num = 100 + undefined; // Prints NaN
document.write("The value of the num is: " + num + "<br>");
num = false * undefined; // Prints NaN
document.write("The value of the num is: " + num + "<br>");
</script>
</body>
</html>
显式类型转换
很多情况下,程序员需要手动转换变量的数据类型。这称为显式类型转换。
在 JavaScript 中,您可以使用构造函数或内置函数来转换变量的类型。
转换为字符串(显式转换)
您可以使用 String() 构造函数将数字、布尔值或其他数据类型转换为字符串。
String(100); // 数字转换为字符串 String(null); // null 转换为字符串 String(true); // 布尔值转字符串
示例
您可以使用 String() 构造函数将值转换为字符串。您还可以使用 typeof 运算符检查结果值的类型。
<html>
<head>
<title> Converting to string explicitly </title>
</head>
<body>
<script>
document.write(typeof String(100) + "<br/>");
document.write(typeof String(null)+ "<br/>");
document.write(typeof String(true) + "<br/>");
</script>
</body>
</html>
我们还可以使用 Number 对象的 toString() 方法将数字转换为字符串。
const num = 100; num.toString() // 将 100 转换为 '100'
转换为数字(显式转换)
您可以使用 Numer() 构造函数将字符串转换为数字。我们还可以使用 一元加号 (+) 运算符将字符串转换为数字。
Number('100'); // 将 '100' 转换为 100
Number(false); // 将 false 转换为 0
Number(null); // 将 null 转换为 0
num = +"200"; // 使用一元运算符
但是,您也可以使用以下方法和变量将字符串转换为数字。
| 序号 | 方法/运算符 | 描述 |
|---|---|---|
| 1 | parseFloat() | 从字符串中提取浮点数。 |
| 2 | parseInt() | 从字符串中提取整数。 |
| 3 | + | 这是一个一元运算符。 |
示例
您可以使用 Number() 构造函数或一元 (+) 运算符将字符串、布尔值或任何其他值转换为数字。
Number() 函数还可以将数字的指数表示法转换为十进制数。
<html>
<head>
<title> Converting to string explicitly </title>
</head>
<body>
<script>
document.write(Number("200") + "<br/>");
document.write(Number("1000e-2") + "<br/>");
document.write(Number(false) + "<br/>");
document.write(Number(null) + "<br/>");
document.write(Number(undefined) + "<br/>");
document.write(+"200" + "<br/>");
</script>
</body>
</html>
转换为布尔值(显式转换)
您可以使用 Boolean() 构造函数将其他数据类型转换为布尔值。
Boolean(100); // 将数字转换为布尔值 (true)
Boolean(0); // 0 为假值 (false)
Boolean(""); // 空字符串为假值 (false)
Boolean("Hi"); // 将字符串转换为布尔值 (true)
Boolean(null); // null 为假值 (false)
示例
您可以使用 Boolean() 构造函数将值转换为布尔值。所有假值(例如 0、空字符串、null、undefined 等)都会转换为 false,其他值则会转换为 true。
<html>
<head>
<title> Converting to string explicitly </title>
</head>
<body>
<script>
document.write(Boolean(100) + "<br/>");
document.write(Boolean(0) + "<br/>");
document.write(Boolean("") + "<br/>");
document.write(Boolean("Hi") + "<br/>");
document.write(Boolean(null) + "<br/>");
</script>
</body>
</html>
将日期转换为字符串/数字
您可以使用 Date 对象的 Number() 构造函数或 getTime() 方法将日期字符串转换为数字。数字日期表示自 1970 年 1 月 1 日以来的总毫秒数。
请按照以下语法将日期转换为数字。
Number(date); 或 date.getTime();
您可以使用 String() 构造函数或 toString() 方法将日期转换为字符串。
请按照以下语法将日期转换为字符串。
String(date); 或 date.toString();
让我们尝试借助程序来演示这一点。
<html>
<head>
<title> Coverting date to string / number </title>
</head>
<body>
<script>
let date = new Date();
let numberDate = date.getTime();
document.write("The Numeric date is: " + numberDate + "<br/>");
let dateString = date.toString();
document.write("The string date is: " + dateString + "<br/>");
</script>
</body>
</html>
JavaScript 转换表
下表列出了原始值以及将其转换为字符串、数字和布尔值后的结果。
| 值 | 字符串转换 | 数字转换 | 布尔值转换 |
|---|---|---|---|
| 0 | "0" | 0 | false |
| 1 | "1" | 1 | true |
| "1" | "1" | 1 | true |
| "0" | "0" | 0 | true |
| "" | "" | 0 | false |
| "Hello" | "Hello" | NaN | true |
| true | "true" | 1 | true |
| false | "false" | 0 | false |
| null | "null" | 0 | false |
| undefined | "undefined" | NaN | false |
| [50] | "50" | 50 | true |
| [50, 100] | "[50, 100]" | NaN | true |

