JS中强制类型转换

JavaScript提供了3种强制类型转换的方法

一、Boolean()方法

  该方法将指定的参数转换成布尔型。Boolean(object)。参数object可以是字符串对象、数值对象、DOM对象等。

  以下是Boolean()方法返回false的6中情况。其他参数均为true

返回值为false
数据 说明
Boolean(0) 参数0,false
Boolean(null) 参数null,false
Boolean(undefined) 参数undefined型,false
Boolean("") 参数空字符串,false
Boolean(false) 参数false,false
Boolean(NaN) 参数NaN,false  

二、Number()方法

  和parseInt()方法,parseFloat()方法类似,参照:

三、String()方法

  String()  方法和  toString()  方法类似,但是  tostring()  无法转换  null  和  undefined。

1         var a = null ;
2         var b;
3         //String()
4         document.write(String(a)+"<br>");
5         document.write(String(b));
6         //toString()
7         document.write(a.toString());
8         document.write(b.toString());

String()方法:

tostring()方法:

原文地址:https://www.cnblogs.com/mwxz/p/13297766.html