1.string -> number

  1. // Number() parseInt() parseFloat()
  2. // string --> number
  3. // 1.能够识别纯数字的字符串
  4. // 2.""," " 识别成0
  5. // 3.NaN
  6. console.log(parseInt('13px'));//13
  7. console.log(parseFloat('13.2222px'));//13.2222
  8. console.log(Number("12.2"));//12.2
  9. console.log(Number(''));//0
  10. console.log(Number(' '));//0
  11. console.log(Number('12px'));//NaN

2.boolean -> number

  1. // boolean --> number
  2. // true -> 1 false -> 0
  3. console.log(Number(true));//1
  4. console.log(Number(false));//0

3.any-boolean

  1. // number--boolean
  2. // 0 - false 1 - true
  3. console.log(Boolean(0));
  4. console.log(Boolean(1));
  5. // string -- boolean
  6. // ""(空字符串) - false
  7. // " "(有空格的字符串) - true
  8. console.log(Boolean(''));
  9. console.log(Boolean(' '));

4.any - boolean

  1. // 转为字符串的方法
  2. // 1.String
  3. // 2.toString
  4. // 3. + ""
  5. var a = 20
  6. console.log(a);
  7. console.log(String("10"));
  8. console.log(a.toString());
  9. console.log(a + "");