判断js中的数据类型

原文:https://zhidao.baidu.com/question/1896943434117294300.html

最常见的判断方法:typeof
alert(typeof a) ------------> string
其中typeof返回的类型都是字符串形式,需注意,例如:
alert(typeof a == "string") -------------> true
alert(typeof a == String) ---------------> false
判断已知对象类型的方法: instanceof
alert(c instanceof Array) ---------------> true
alert(d instanceof Date)
alert(f instanceof Function) ------------> true
alert(f instanceof function) ------------> false
注意:instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。

猜你喜欢

转载自blog.csdn.net/xm_csdn/article/details/78235198