3-1 string,boolean —>number Number(value)

3-1-1 string —> number

Number(“ “) Number(“”) Number(null) => 输出 0

  1. // string --> number 只能识别纯数字的字符串
  2. // Number(" ") Number("") Number(null) => 输出 0
  3. var str = "10";
  4. var s = "hello world"
  5. var z = "2121sgd"
  6. var num = Number(str)
  7. console.log(num) // 10
  8. console.log(Number(s)) // NaN 不是一个数字
  9. console.log(Number(z)) // NaN

3-1-2 boolean —> number

  1. var b = true
  2. var f = false
  3. console.log(Number(b)) // 1
  4. 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:true
    1. var a = 0;
    2. var b = -1;
    3. var c = 20;
    4. console.log(Boolean(a)) // false
    5. console.log(Boolean(b)) // true
    6. console.log(Boolean(c)) // true

    3-2-2 string —> boolean

    特点:空字符串””为:false 其余都为:true
    1. var str = "hello world"
    2. var str1 = " "
    3. var str2 =""
    4. console.log(Boolean(str)) // true
    5. console.log(Boolean(str1)) // true
    6. console.log(Boolean(str2)) // false

    3-3 任意类型转为number parseInt() parseFloat()

    1. var str = "12px";
    2. var str2 = "12.12121px"
    3. console.log(parseInt(str))
    4. console.log(parseFloat(str2))

    3-4 任意类型转为string String() toString()

    1. var a = true
    2. var str = true+""
    3. console.log(typeof str)
    4. console.log(typeof String(a))
    5. console.log(typeof a.toString())