JavaScript的5中基本数据类型

JS包含五种基本数据类型:undefind、string、number、boolean、Object;其中undefind、string、number、boolean属于值类型,Object属于引用类型;为了更好的理解JS基本的数据类型,我在下面列举了一些例子,希望能帮助大家更好的理解。

例子1:

//如何判断var定义的数据类型(), 使用typeof来区分四种值类型的区别;

var abc;

var num=1;

var str = "zifuchuan";

var bool = false;

console.log(typeof abc, typeof num, typeof str, typeof bool) ;// undefined number string boolean

//为什么不用typeof来区分是不是Object

var obj = {};

var arr = [];//Array数组,JS内置对象

console.log(typeof obj, typeof arr);//object object function

//从打印结果来看无法区分是数组还是对象。

例子2:

//instanceof

console.log(obj instanceof Object,arr instanceof Array);//true true

猜你喜欢

转载自blog.csdn.net/oracle11770/article/details/80070642