3-1 string,boolean—>number()只能识别纯数字的字符串

  1. //string-->number
  2. var str = "123";
  3. var s = "123px";
  4. var a = "";//空串或者全是空格的字符
  5. console.log(Number(a)); //0
  6. console.log(Number(str));//123
  7. console.log(Number(s));//NaN
  8. *console.log(Number(NaN));//NaN
  9. // boolean-->number
  10. console.log(Number(true)); //1
  11. console.log(Number(false)); //0

3-2 string,number—>boolean()

  1. //number-->Boolean()
  2. // 特殊:0-->false;
  3. var b = 0;
  4. console.log(Boolean(b)); //false
  5. //string-->Boolean()
  6. // 特殊:"" -->false;
  7. var d = "";
  8. console.log(Boolean(d));//false

3-3 Any-string

  1. String(); toString(); +"";
  2. var a = 123;
  3. var b = 45 + "";
  4. console.log(a.toString());//"123"
  5. console.log(String(a));//"123"
  6. console.log(b);//"45"

3-4 Any-number

  1. Number(); parseInt(); parseFloat();
  2. var a = "12d3px";
  3. var b = "12.34px"
  4. console.log(parseInt(a));//12
  5. console.log(parseFloat(b));//12.34

3-5 Any-boolean

  1. boolean();