js 面试题总结

1.typeof返回数据类型有哪些
  number,string,boolean,object,undefined,function
2.强制类型转换有哪些?隐式类型转换有哪些?
  转换为string:toString(),
  转换为number:Number(),

         parseInt()   parseFloat()
  转换为Boolean:

Number("1") // 1
Number("1a") //NaN
Number("") //空字符串 0
Number(" ") // 空格 0

console.log(parseInt("123px")) //123
console.log(parseInt("123px")) //123


var a=true;
a=parseInt(b);
console.log(typeof a);  //number
console.log(a);  //NaN
//如果对非string使用parseInt()或parseFloat()它会先将其转换为string,然后再操作

var a= 123;
a=Boolean(a);
console.log(typeof a);   // boolean
console.log(a);  //true
//除了0、NaN、空串、null、undefined其余都是true。对象也会转换为true

 隐式类型转换:

//隐式转换 + - * == / 
10 + '20'    //2010
10 - '20'    //-10
10 - 'one'   //NaN
10 - '100a'  //NaN
10*'20'      //200
'10'*'20'    //200
20/'10'      //2
'20'/'10'    //2
'20'/'one'  //NaN
undefined == null;    //true
'0' == 0;            //true,字符串转数字
0 == false;           //true,布尔转数字
'0' == false;       //true,两者转数字
null == false;       //false
undefined == false;  //false

1.undefined等于null

2.字符串和数字比较时,字符串转数字

3.数字为布尔比较时,布尔转数字

4.字符串和布尔比较时,两者转数字

false=0 true=1

 

 



posted @ 2022-02-24 13:16  lilyliu329  阅读(54)  评论(0编辑  收藏  举报