日期:2014-05-16  浏览次数:20354 次

浅谈javascript的五种基本数据类型

? ? ?在javascript中申明变量使用的关键字都是var,这点与其他的编程语言不尽相同,但是javascript亦含有五种基本的数据类型(也可以说是简单数据类型),它们分别是:Undefined,Null,Boolean,Number和String。还含有一种复杂数据类型—Object。这里我们不谈复杂数据类型。

1、typeof
? ? ?typeof这个关键字是一定要说的,因为javascript是松散类型的,在变量申明时并没有使用与之类型相对应的关键字,如果在代码中想要获知某个变量的基本数据量,就可以使用typeof。这里要注意的是typeof返回的是字符串类型。

(1)、"undefined"——未申明,或者变量的值即为undefined或者未初始化;

(2)、"boolean" ? ——如果这变量的值是布尔类型;

(3)、"string" ? ? ? ——值是字符串类型;

(4)、"number" ? ?——值是数字类型;

(5)、"object" ? ?——对象或者值为null;

(5)、"function" ? ?——函数。

?

?例如:

var testString = "Hello";
var testBoobean = true;
var testUndefined = undefined;
var testUndefined1;
var testNull = null;
var testObject = {a:1};
var testFunction = function(){return;};

alert(testString);//"string"
alert(testBoobean);//"boolean"
alert(testUndefined);//"undefined"
alert(testUndefined1);//"undefined"
alert(testUndefined2);//"undefined"
alert(testNull);//"object"
alert(testObject);//"object"
alert(testFunction);//"function"
?

? ? ? 在js中函数也是对象,但是函数又要一些区别与其他对象的特定,所以ECMAScript在使用typeof的时候将其区分开来。

?

2、Undefined

? ? ? 这是一个很有意思的数据类型,因为它的值只有一个,那就是undefined。在申明变量时如果没有将变量赋值的话这个变量也是属于Undefined类型的。

?

例子:
var testUndefined;
var testUndefined1 = undefined;

//申明的时候没有赋值,那么解析器会自动给其赋值为undefined,所以输出为true
alert(testUndefined == undefined)//true
alert(testUndefined1 == undefined)//true
//根据上面所说的typeof返回的是字符串"undefined"所以输出为true
alert(typeof testUndefined == "undefined")//true

?

? ? ? 如果一个变量没有申明就直接去访问解释器会报错误信息,但是这样的变量如果使用typeof返回的结果也是"undefined"。

?

3、Null

? ? ? Null也是一个只有一个值得数据类型,它的值就是null,任何变量只要给其赋值为null的话这个变量的数据类型就是Null类型。null值表示控对象指针,所以申明的变量要是想用来保存对象并且在申明之初还不能确定具体保存哪个对象的时候就将其赋值为null,在使用的时候只要检查该变量是否为null就可以知道该变量是否保存了对象。

?

例如:
var testNull = null;

//对null使用typeof返回的是"object"
alert(typeof testNull); //"object"

alert(testNull != null); //false

testNull = o {a:1};
alert(testNull != null);//true
?

? ? ? 有上面的代码可以很清楚的看出如果将变量初始化为null那么只要判断其值是否为null就知道该变量是否保存了对象的引用,当然事先申明为null的变量也可以保存基本类型数据。

?

? ? ? 其实null和undefined还是有点微妙的关系的,在javascript中undefined值派生自null,因此ECMA-26规定了它们的相等性测试为true。

?

例如:
alert(undefined == null)//true

var testNull = null;
var testUndefined = undefined;

alert(testNull == null)//true
alert(testNull == undefined)//true

alert(testUndefined == undefined)//true
alert(testUndefined == null)//true
?

在实际的编程中我们没有必要将一个变量赋值为undefined,但是却有很多情况下要将变量赋值为null,将变量赋值为null可以便于我们将它与undefined区分也便于垃圾回收处理。

?

4、Boolean

在javascript中Boolean类型用还是比较多的一种简单数据类型,它有两个值,分别是true和false,因为在javascript中字母是区分大小写的,所以True和False不是Boolean的值。

可以通过如下方式给Boolean类型的变量赋值:

?

var testBoolean = true;

var testBoolean1 = false;

?

?调用Boolean()方法可以将任何类型的值转化成与之相对应的Boolean类型的值,也就是可以将其转化成true或者false。

?

例如:
//将非空字符串转化成true
alert(Boolean("a"))//true
//将空字符串转化成false
alert(Boolean(""))//false
//将对象转化成true
alert(Boolean({a:"a"}))//true

?

?

将各种类型的值转化成Boolean类型的规则如下:

?

?

友情链接: 爱易网 云虚拟主机技术 云服务器技术 程序设计技术 开发网站 APP开发教程
Copyright © 2013-2024 爱易网页 当前在线:702人  网站在10时35分28秒内访问总人数:110037人 当前 21.17%  粤ICP备18100884号-2
数据类型 转化成true的值 转化成false的值
Boolean ? ? ? ? ? ture false
String 所有的非空字符串 ? ? ? ? ? ? ? ? ""(空字符串)
Number 任何非零数字(包括无穷大) ? ? ? ? ? ? 0和NaN ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
Object 任何对象 不存在