1.强制转换

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();
  3. var str = "12px";
  4. var str2 = "12.12121px"
  5. console.log(parseInt(str))
  6. 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
  2. var str = "hello world"
  3. var str1 = " "
  4. var str2 =""
  5. console.log(Boolean(str)) // true
  6. console.log(Boolean(str1)) // true
  7. console.log(Boolean(str2)) // false

3、Any-String

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