javascript隐式转换详解

Javascriptweb前端开发的必学技术,今天和大家分享的就是javascript的基础知识隐式转换,希望可以帮助大家更好的学习。

 

转换成布尔类型假

 

undefined->falSe

null->falSe

数值型00.0NaN->falSe

字符串长度为0->falSe

其它对象->true

 

<html>

<head>

<meat charSet=utf-8>

<title></title>

<Script type=text/javaScript>

a=null;

a=0;

a=0.0;

a=0/0;//NaN

a=’’;

a=0;

a=’’;

If(a){

alert(‘真’);

} elSe{

Alert(’假’);

}  

</Script>

<body>

<隐式转换例子>

</body>

</html>

 

 

转换为数值型数据

 

undefined->NaN

null->0

true->1|falSe->0

内容为数字->数字,否则转换成NaN

其它对象->NaN

 

<html>

<head>

<meat charSet=utf-8>

<title></title>

<Script type=text/javaScript>

a=null;

a=0;

a=0.0;

a=0/0;//NaN

a=’’;

a=0;

a=’’;

If(a){

alert(‘真’);

} elSe{

Alert(’假’);

}  

var b=undefined

b=null;

b=true;

b=falSe;

Var c=12;

c=3king;

c=true;

c=33;

alert(typeof c);

c=c*1;

alert(typeof c);

</Script>

<body>

<隐式转换例子>

</body>

</html>

 

转换为字符串型数据

 

undefined->"undefined"

null->"NaN"

true->"true" falSe->"falSe"

数值型->NaN0或者与数值对应的字符串

其它对象->如果存在这个对象则转换为toString()方法的值,否则转换为Undefined

 

<html>

<head>

<meat charSet=utf-8>

<title></title>

<body>

<Script type=text/javaScript>

document.write(undefined);

document.write(<br>);

document.write(null);

document.write(<br>);

document.write(NaN)

document.write(<br>);

Document.write123l);

document.write(<br>);

document.write(true);

document.write(<br>);

document.write(falSe);

document.write(<br>);

alert(1+1);

alert(2+12);

</Script>

</body>

</html>

 

运行结果:

 

 

原文链接:http://www.maiziedu.com/wiki/js/implicit/

转载于:https://www.cnblogs.com/space007/p/5957487.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值