js中== 和===中的区别

时间:2023-03-08 22:16:05
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script>
(function() {
//js中== 和===中的区别
var num = 1;
var b = true;
console.log(num == b);//true,b先转换为1,然后比较
console.log(typeof num);
console.log(num === b);//false var str = "1";
console.log(num == str);//true
console.log(num === str);//false })(); //从以上可以看出,js中==比较的两个数如果类型不相同,会转换为相同的类型,然后再比较值是否相同
//而===是先比较类型是否相同,不会自动转换类型,只有在类型相同的情况下才会比较是否相同,相当于强等于

</script>
</head>
<body> </body>
</html>