JavaScript基础语法(类型转换)
使用==运算符的时候会发生类型转换。
其他类型转为number
-
string转换为number类型:按照字符串的字面值,转为数字。如果字面值不是数字,则转为NaN将
string转换为number有两种方式:- 使用
+正号运算符:
- 使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var str = + "20";
document.write(str + 1) //21
</script>
</body>
</html>
运行结果

使用 parseInt() 函数(方法):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var str = "20";
document.write(parseInt(str) + 1);
</script>
</body>
</html>
运行结果

boolean转换为number类型:true 转为1,false转为0
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var flag = +false;
document.write(flag); // 0
</script>
</body>
</html>
运行结果

其他类型转为boolean
- number 类型转换为 boolean 类型:0和NaN转为false,其他的数字转为true
- string 类型转换为 boolean 类型:空字符串转为false,其他的字符串转为true
- null类型转换为 boolean 类型是 false
- undefined 转换为 boolean 类型是 false
















![[Java反序列化]—CommonsCollections5](https://img-blog.csdnimg.cn/69fcc43e7bc14f47b1b75f6812cfcbfb.png)
![[附源码]Python计算机毕业设计Django-大学生健康档案管理](https://img-blog.csdnimg.cn/186ad9862faf4449b0048e5730296fac.png)

