1、 string,boolean—>number

1-1、 纯数字string—>number

  1. 特点:只能识别纯数字的字符串

Number(value)

  1. var a = "10";
  2. var str = "hello"
  3. console.log(Number(a)) //10
  4. console.log(Number(str)) //NaN 不是一个数字
  5. console.log(Number("3.0")) //3
  6. console.log(Number(undefined)) // NaN

1-2、 boolean —>number

  1. true --> 1
  2. false -->0

1-3、12px或12.34px这样的string—>number

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

2、number,string—>boolean

Boolean();

2-1、 number—boolean

  1. 特殊:0-->false,其他情况都为true
  2. var a = 0;
  3. var b = 10;
  4. console.log(Boolean(a)) //false
  5. console.log(Boolean(10)) //true

2-2、string—boolean

  1. 特殊:""-->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、Any-String

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

Tips:在加运算中,只要一边为字符串,结果一定为字符串