Javascript数据类型

79 阅读4分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第13天,点击查看活动详情

JavaScript数据类型

JavaScript中数据的类型大体可以分为三种:基本类型、特殊类型、复杂类型。

一.基本类型

类型type
字符型string
数值型number
布尔型boolean
js中的变量是一种动态数据类型,也就是变量本身是没有数据类型的,只有赋值以后才有了类型。
var a="张三";
	a=25;
	a=true;

1.字符型(string)

1.表示文本。由Unicode字符、数字、标点符号组成的序列。 2.首尾由单引号或双引号括起。 3.特殊字符需要转义符(\),如:\n、\\、\'、\"。

var a="\u4f60\u597d啊222\n欢迎\\来到\"JavaScript世界\"";
alert(a);
你好啊222
欢迎\来到"JavaScript世界"

2.数值型(number)

var b = 5;
alert(b + b);//10

3.布尔型(boolean)

仅有两个值true和false。也代表1和0。实际运算中true==1、false==0。

var c = true;
var c2 = false;
alert(c + c);//2
alert(c + c2);//1
alert(c2 + c2);//0

二.特殊类型

类型type
未初始化undefined
空值null

1.未初始化(undefined)

var d;
alert(d);//undefined

2.空值(null)

var e = null;
alert(e);//null

三.复杂类型

类型type
数组Array
对象Object

1.数组(array)

var cnweek = new Array(7);//创建指定长度的数组
var books = new Array();//创建未指定长度的数组

2.对象(object)

1.自定义对象:我们自己根据需要创建的对象。 2.内置对象:JavaScript已经创建好了的对象,我们直接拿来用就行。

四.数据类型的判定

1.判定不是数值

isNaN():NaN(not a number),判断不是数字。true(不是数字),false(是数字)。

alert(isNaN("10"));       //false - can be converted to number 10。
alert(isNaN(22.5));       //false - 22.5 is a number。
alert(isNaN("1234blue")); //true - cannot be converted to a number。
alert(isNaN(NaN));        //true
alert(isNaN(""));         //false - 给一个空字符串,没必要给你转,故返回false。	
alert(isNaN(true));       //false - can be converted to number。

2.判定具体类型

在js中判断一个变量的数据类型可以使用 typeof 运算符

/* 1.基本类型 */
var name = "张三";
console.log("typeof name: " + typeof name); //string
var num = 20;
console.log("typeof num: " + typeof num); //number
var blen = true;
console.log("typeof blen: " + typeof blen); //boolean

/* 2.特殊类型 */
var age;
console.log("typeof age: " + typeof age); //undefined
var n = null;
console.log("typeof n: " + typeof n); //object-是一个历史错误(JS的发明者Brendan Eich自己也是这样说的)

/* 3.复杂类型 */
var cnweek = new Array(7);
console.log("typeof cnweek: " + typeof cnweek); //object-数组就是对象
var obj = {} //自定义一个空对象
console.log("typeof obj: " + typeof obj); //object
console.log("typeof window: " + typeof window); //object
console.log("typeof document: " + typeof document); //object

/* 4.函数 */
function firstMethod() {
    alert("my first method!");
}
console.log("typeof firstMethod: " + typeof(firstMethod)); //function-函数就是一种特殊的对象。typeof没设计好。

六.数据类型的转换

1.其他类型->字符串类型

/* 1.数值型转换成字符型 */
var num = 10;
var num1 = num.toString();
var num2 = String(num);
var num3 = num + ''; //使用 + 实现隐式转换
console.log("num:" + num + "; typeof num: " + typeof num);
console.log("num1:" + num1 + "; typeof num1: " + typeof num1);
console.log("num2:" + num2 + "; typeof num2: " + typeof num2);
console.log("num3:" + num3 + "; typeof num3: " + typeof num3);

/* 2.布尔型转换成字符型 */
var blen = true;
var blen1 = blen.toString();
var blen2 = String(blen);
var blen3 = blen + ''; //使用 + 实现隐式转换
console.log("blen:" + blen + "; typeof blen: " + typeof blen);
console.log("blen1:" + blen1 + "; typeof blen1: " + typeof blen1);
console.log("blen2:" + blen2 + "; typeof blen2: " + typeof blen2);
console.log("blen3:" + blen3 + "; typeof blen3: " + typeof blen3);

2.其他类型->数字类型

/* 1.字符型转换成数值型 */
var str = '20';
// str = '20.3';
// str = 'abc';
// str='20abc';
// str = 'abc20';
// str='北大青鸟';
// str='20北大青鸟';
// str='北大青鸟20';
var str1 = parseInt(str); //强制转换成整数。如果不能转换,则返回NaN。例如:parseInt("6.12")=6。
var str2 = parseFloat(str); //强制转换成浮点数。如果不能转换,则返回NaN。例如:parseFloat("6.12")=6.12。
var str3 = Number(str);
var str4 = str - 0; //使用 - 实现隐式转换
var str5 = str * 1; //使用 * 实现隐式转换
var str6 = str / 1; //使用 / 实现隐式转换
console.log("str:" + str + "; typeof str: " + typeof str);
console.log("str1:" + str1 + "; typeof str1: " + typeof str1);
console.log("str2:" + str2 + "; typeof str2: " + typeof str2);
console.log("str3:" + str3 + "; typeof str3: " + typeof str3);
console.log("str4:" + str4 + "; typeof str4: " + typeof str4);
console.log("str5:" + str5 + "; typeof str5: " + typeof str5);
console.log("str6:" + str6 + "; typeof str6: " + typeof str6);

/* 2.布尔型转换成数值型 */
var blen = true;
var blen1 = parseInt(blen); //强制转换成整数。如果不能转换,则返回NaN。例如:parseInt("6.12")=6。
var blen2 = parseFloat(blen); //强制转换成浮点数。如果不能转换,则返回NaN。例如:parseFloat("6.12")=6.12。
var blen3 = Number(blen);
var blen4 = blen - 0; //使用 - 实现隐式转换
var blen5 = blen * 1; //使用 * 实现隐式转换
var blen6 = blen / 1; //使用 / 实现隐式转换
var blen7 = blen + 0; //使用 + 实现隐式转换
console.log("blen:" + blen + "; typeof blen: " + typeof blen);
console.log("blen1:" + blen1 + "; typeof blen1: " + typeof blen1);
console.log("blen2:" + blen2 + "; typeof blen2: " + typeof blen2);
console.log("blen3:" + blen3 + "; typeof blen3: " + typeof blen3);
console.log("blen4:" + blen4 + "; typeof blen4: " + typeof blen4);
console.log("blen5:" + blen5 + "; typeof blen5: " + typeof blen5);
console.log("blen6:" + blen6 + "; typeof blen6: " + typeof blen6);
console.log("blen7:" + blen7 + "; typeof blen7: " + typeof blen7);

3.其他类型->布尔类型

var data = 0; //false
// data = 1; //true
// data = 2; //true
// data = 2.3; //true
// data = -2.3; //true
// data = NaN; //false
// data = ''; //false
// data = undefined; //false
// data = null; //false
var data1 = Boolean(data);
console.log("data:" + data + "; typeof data: " + typeof data);
console.log("data1:" + data1 + "; typeof data1: " + typeof data1);