string,boolean —>number Number(value)
string —> number
// string --> number 只能识别纯数字的字符串
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
boolean —> number
var b = true
var f = false
console.log(Number(b)) // 1
console.log(Number(f)) // 0
number,string —>boolean Boolean(value)
**undefined,"",NaN,0 --> false**
3-2-1 number —> boolean
特点: 0:false 非0:true
var 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 其余都为:true
var str = "hello world"
var str1 = " "
var str2 =""
console.log(Boolean(str)) // true
console.log(Boolean(str1)) // true
console.log(Boolean(str2)) // false
任意类型转为number parseInt() parseFloat()
var str = "12px";
var str2 = "12.12121px"
console.log(parseInt(str))//12
console.log(parseFloat(str2))//12.12121
任意类型转为string String() toString()
var a = true
var str = true+"123"
console.log(str)//true123
console.log(typeof str)//string
console.log(String(a))//true
console.log(typeof a.toString())//string