number,string,boolean
强制转换
string,boolean—>number Number(value)
string—>number 只能识别纯数字的字符串
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title></head><body><script>// number,string,boolean// 强制转换// string,boolean-->number Number(value)/* string-->number 只能识别纯数字的字符串 */var str = "10";var s = "hello world";var t = "2121fsf";var num = Number(str);console.log(num);console.log(Number(s)); //NaN 不是一个数字console.log(Number(t));</script></body></html>
number,string转boolean
number,string—> Boolean(value)
特殊:0—>false;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//特殊:0-->false;
//特殊:""-->false;
var a = 0;
var b = -1;
var c = 10;
var d = "hello world";
var e = " ";
var f = "";
console.log(Boolean(a));
console.log(Boolean(b));
console.log(Boolean(c));
console.log(Boolean(d));
console.log(Boolean(e));
console.log(Boolean(f));
</script>
</body>
</html>
Boolean-number
boolean—>number
true—>1
false —>0
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var b =true;
var f =false;
console.log(Number(b));
console.log(Number(f));
</script>
</body>
</html>
