编程无涯乐在其中

导航

js

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
/*
6种常用的数据类型,查看变量的数据类型使用typeof
1.undefined 声明一个变量并没有为其赋值
2.null 声明一个变量,并为其赋值为null
3.boolean 声明一个变量,为其赋值为ture或false
4.string 声明一个变量,为其赋值为字符串(单引号和双引号都可以)
5.number 数值型 1.整型 、 2.浮点型 、 3.正负无穷大 、 4.NaN = Not a Number
6.object 对象型
*/

function testDataType() {
var a;
console.log(typeof a, a);

var b = null;
console.log(typeof b, b);//打印object,但是它是null类型

var c = true;
console.log(typeof c, c);

var d = '123';
console.log(typeof d, d);

var e1 = 123; //整型
console.log(typeof e1, e1);
var e2 = 123.45; //浮点型
console.log(typeof e2, e2);
var e3 = 1/0; //Infinity无穷大
console.log(typeof e3, e3);
var e4 = Number("haha"); //NaN = Not a Number
console.log(typeof e4,e4)

var f = new Array();
console.log(typeof f,f);
}
// testDataType();
//自动转型
function transferNumber(){
var b1 = false;
var n1 = Number(b1);
console.log(n1);//0

var c = null;
var n2 = Number(c);
console.log(n2);//0

var e;
var n3 = Number(e);
console.log(n3);//NaN

var f = "fadsf123123adfds";
var n4 = Number(f);
console.log(n4);//NaN


var nullstr = "";
var n5 = Number(nullstr);
console.log(n5);//0

}

transferNumber();

function transferString(){
var a = null;
var b;
console.log(String(a));
console.log(String(b));

var c = true;
console.log(String(c));
}
// transferString();

function transferBoolean(){
var a = "";
var b;
var c = null;
var d = 0;
var e = Number("haha");

console.log(Boolean(a));
console.log(Boolean(b));
console.log(Boolean(c));
console.log(Boolean(d));
console.log(Boolean(e));

console.log(Boolean("kjhasdkjfhs"));
console.log(Boolean(123));

}

// transferBoolean();

</script>
</head>
<body>
</body>
</html>

posted on 2019-04-29 19:16  编程无涯乐在其中  阅读(103)  评论(0编辑  收藏  举报