JavaScript中类型转换中“ +”运算符的重要性是什么?
'+'运算符有很多用途,包括加法,串联和最重要的类型转换。通常,我们使用'+'运算符进行加法,有时使用级联。它在类型转换中也有很多用途。将字符串转换为数字非常容易。
示例
<html>
<body>
<script>
var num = "23";
int = +num;
document.write(int);
document.write("</br>");
document.write(typeof int);
</script>
</body>
</html>输出
23 number
不仅类型转换,此运算符还用于将布尔值 转换为数字。它将布尔值(例如true 或false) 分别更改为1和0。
示例
<html>
<body>
<script>
document.write(+true);
document.write("</br>");
document.write(+false);
</script>
</body>
</html>输出
1 0