js隐式类型转换

笔试题:

var x = 1;
  if (function () {}) {
      x += typeof f;
  }
  console.log(x);
1undefined

解析:

 //if (function f() {}) -->function f() {}作为表达式,function f消失,    
 // 所以在if里面,f为未声明
  //任何一个变量,未经声明使用都会报错。
  //!但是,typeof其时,不会报错,而是undefined,typeof是唯一不报错的操作符
  
  console.log(typeof f);
  console.log(typeof (typeof f));

  console.log(1 + undefined);
undefined
string
NaN

另外,使用一个对象没有的属性,控制台显示undefined,而不是像没声明的变量一样报错error。

易记错的类型转换:

 //alert(typeof (undefined));// ->undefined
  //alert(typeof (NaN));//->number
  //alert(typeof (null));//->object
  
   var a = "123abc";
  //alert(typeof (+a));/*->NaN->numnber*/
  //alert(typeof (!!a));//->boolean/*判断运算符的结果*/
  
//alert("11" + 11);//->1111
//alert(typeof (typeof (a)));//->string
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值