【JavaScript的五种基本数据类型及转换】

时间:2021-01-14 16:47:06

js中有六种数据类型,包括五种基本数据类型(Number,String,Boolean,Null,Undefined),和一种混合数据类型就是特殊的(Object)。

"undefined" 变量未定义
"boolean" 变量是布尔值
"string" 变量是字符串
"number" 变量是数值
"object" 变量是对象或者null
"function" 变量是函数

typeof  操作符可以检测变量的数据类型(输出的是一个关于数据类型的字符串)。

 //          typeof是得到变量的类型(查看数据类型)
var a; //undefined
a=null; //object
a=; //number
a=NaN; //number
a=""; //string(带引号的基本都是字符串)
a=false; //boolean布尔
a=''; //string(带引号的基本都是字符串)
alert(typeof a);

隐式转换:

以下是一些例子:

其中:

其它类型转换成数值型:
 数值型+undefined=NaN   
数值型+null=数值
布尔boolean:true+2=3 false+2=2
 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
/*
if(exp){
exp为true的代码段;
}else{
exp为false的代码段;
}
*/
//其它类型转换成布尔类型假的有
// var a;//undefined->false
// typeof得到变量的类型
// alert(typeof a);
// a=null;//null->false
// 0 0.0 NaN->false
// a=0;
// a=0.0;
// a=0/0;
// a=NaN;
a=null;
alert(a);
// a='';//空字符串->false
// a='0';
// a=' ';
// alert(typeof a);
// if(a){
// alert('真');
// }else{
// alert('假');
// }
//其它类型转换成数值型
var b=undefined;//undefined->NaN
b=null;//null->0
b=false;//true->1
// b=false;//false->0
// alert(1+b); // 数值型+undefined=NaN 数值型+null=数值 ( boolean:true+2=3 false+2=2)
var c='';//'12'->12
// c='3king';//'3king'->NaN
c=false;
// alert(2*c);
// c='33';
// alert(typeof c);
c=c*;
// alert(typeof c); </script>
</head>
<body>
<h1>隐式转换的例子</h1>
<script type="text/javascript">
//其它类型转换成字符串型
document.write(undefined);//'undefined'
document.write('<br/>');
document.write(null);//'null'
document.write('<br/>');
document.write(NaN);//'NaN'
document.write('<br/>');
document.write();//'123'
document.write('<br/>');
document.write(true);//'true'
document.write('<br/>');
document.write(false);//'false'
document.write('<br/>');
//alert(1+"1");//拼接字符串
//alert('2'+12);//拼接字符串
</script>
</body>
</html>

强制转换:<有五种>

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
//其它类型转换成布尔类型false的有
var test=Boolean();
test=Boolean(-);
test=Boolean(NaN);
test=Boolean(undefined);
test=Boolean('');
test=Boolean(0.0);
test=Boolean('');
//其它类型转换成字符串型
test=String();
test=String(23.34);
test=String('this is a test');
test=String(true);
test=String(false);
test=String(null);
test=String(undefined);
test=String(NaN);
//其它类型转换成数值型
test=Number();
test=Number(232.3);
test=Number(true);
test=Number(false);
test=Number(undefined);
test=Number(NaN);
test=Number(null);
test=Number('3king');
test=Number('');
//通过parseInt()进行转换成整型
test=parseInt('');
test=parseInt(,); //234
// alert(test);
test=parseInt('0xabcdef'); //11295375
//alert(test);
test=parseInt(''); //12344
// alert(test);
test=parseInt(,); //69
// alert(test);
test=parseInt('3ki23ng'); //3
// alert(test); //
test=parseInt('true'); //NaN
//alert(test);
test=parseInt(true); //NaN
//alert(test);
test=parseInt(' 35 6 a ');//35
// alert(test);
//通过parseFloat()转换成浮点型
test=parseFloat('123.34abc');
//test=parseFloat('123');
// test=parseFloat('sdf');
// test=parseFloat(' 2e3a');
alert(test);
</script>
</head>
<body>
<h1>强制转换的例子</h1>
</body>
</html>