1、强制数据类型转换

1-1 string,boolean—>number

1-1-1纯数字string—>number
  1. 特点:只能识别纯数字的字符串
  2. Number(value)
  3. var a = "10";
  4. var str = "hello"
  5. console.log(Number(a)) //10
  6. console.log(Number(str)) //NaN 不是一个数字
  7. console.log(Number("3.0")) //3
  8. console.log(Number(undefined)) // NaN

1-1-2 boolean —>number
  1. var t = true
  2. var f = false
  3. console.log(Number(t)); //1
  4. console.log(Number(f)); //0

1-1-3、含有字符的string—>number
  1. parseInt()
  2. parseFloat()
  3. var str = "12px"
  4. var s = "12.34px"
  5. console.log(parseInt(str));
  6. console.log(parseFloat(s));

1-2 number,string—>boolean

1-2-1 number—boolean
  1. Boolean()
  2. 0转换为false,其他都为true

1-2-2 string—>boolean
  1. ""false,其他都为true

1-3 Any-String

  1. String()
  2. toString()
  3. +""