强制转换

1. any-number

1-1 字符转number

  1. var str="12";
  2. console.log(Number(str)) //12
  3. var s = "";
  4. var p = "";
  5. console.log(Number(s))
  6. console.log(Number(p))
  1. string-->number
  2. parsenInt(); "12px";
  3. parseFloat(); "12.434px";
  4. console.log(Number("12px") //NaN

1-2 boolean—>number

  1. Number(true) //1
  2. Number(false) //0

2.any—>boolean

2-1 number—>boolean

  1. tips:0转换为false,其他都为ture

2-2 string—>boolean

  1. tips:""为flase,其他为true

3 any-string

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

自动转换

  1. +,-,*,/,%
  2. a.先将两边(Number)的值转换为number在计算
  3. b.+只要有一边为字符串,那么结果一定是字符串
  4. c.NaN和任何值计算,结果一定是NaN

练习题

Snipaste_2021-09-16_09-27-58.png
b=14 c=100 d=NaN f= 10 g=90