3-1 string,boolean —>number Number(value)
3-1-1 string —> number
Number(“ “) Number(“”) Number(null) => 输出 0
// string --> number 只能识别纯数字的字符串
// Number(" ") Number("") Number(null) => 输出 0
var str = "10";
var s = "hello world"
var z = "2121sgd"
var num = Number(str)
console.log(num) // 10
console.log(Number(s)) // NaN 不是一个数字
console.log(Number(z)) // NaN
3-1-2 boolean —> number
var b = true
var f = false
console.log(Number(b)) // 1
console.log(Number(f)) // 0
3-2 number,string —>boolean Boolean(value)
**undefined,"",NaN,0,null --> false**
3-2-1 number —> boolean
特点: 0:false 非0:truevar a = 0;
var b = -1;
var c = 20;
console.log(Boolean(a)) // false
console.log(Boolean(b)) // true
console.log(Boolean(c)) // true
3-2-2 string —> boolean
特点:空字符串””为:false 其余都为:truevar str = "hello world"
var str1 = " "
var str2 =""
console.log(Boolean(str)) // true
console.log(Boolean(str1)) // true
console.log(Boolean(str2)) // false
3-3 任意类型转为number
parseInt() parseFloat()
var str = "12px";
var str2 = "12.12121px"
console.log(parseInt(str))
console.log(parseFloat(str2))
3-4 任意类型转为string
String() toString()
var a = true
var str = true+""
console.log(typeof str)
console.log(typeof String(a))
console.log(typeof a.toString())