<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
//原始类型:string,number,bool,null,undefined
//当number+bool时,会将bool的true转换成1,false转换成0,颠倒顺序不影响结果
var result = true + 10;
console.log("10 + true = " + result);
//当number+null时,会将null转换成0做加法,颠倒顺序不影响结果
result = 10 + null;
console.log("10 + null = " + result);
//当number+undefined时,结果是NaN,颠倒顺序不影响结果
result = undefined + 10;
console.log("10 + undefined = " + result);
//当undefined+undefined时,结果是NaN
result = undefined + undefined;
console.log("undefined + undefined = " + result);
//当undefined+bool时,结果是NaN
result = undefined + true;
console.log("undefined + true = " + result);
//当undefined+null时,结果是NaN,综上undefined与number,null,bool值做四则运算,结果都是NaN
result = undefined + null;
console.log("undefined + null = " + result);
//当string+number时,结果是string
result = "10" + 10;
console.log('"10" + 10 = ' + result);
//当number+string时,结果还是string
result = 10 + "10";
console.log('10 + "10" = ' + result);
//当number*string时,结果就变成了number,如果string无法转换成number,则结果是NaN
result = 10 * "10";
console.log('10 * "10" = ' + result);
result = 10 * "10g";
console.log('10 * "10g" = ' + result);
//当undefined&&undefined,值仍然为undefined
result = undefined && undefined;
console.log("undefined && undefined = " + result);
//引用类型,对象,数组,函数
//当string+object,结果为string
result = "10" + {};
console.log('"10" + {} = ' + result);
//当number+object,结果也是string
result = 10 + {};
console.log("10 + {} = " + result);
//当number*object,结果为NaN,其实这时候object转换成了一个string,并且该string无法转换成number
result = 10 * {};
console.log("10 * {} = " + result);
//原始类型之间的比较
result = null == undefined;
console.log("null == undefined:" + result);
result = null == 0;
console.log("null == 0:" + result);
result = null == "0";
console.log("null == '0':" + result);
result = null == false;
console.log("null == false:" + result);
result = undefined == 0;
console.log("undefined == 0:" + result);
result = undefined == "0";
console.log("undefined == '0':" + result);
result = undefined == false;
console.log("undefined == false:" + result);
result = "0" == 0;
console.log("'0' == 0:" + result);
result = "0" == false;
console.log("'0' == false:" + result);
result = 0 == false;
console.log("0 == false:" + result);
//事实证明,0,'0',false是两两相等的,和null,undefined都不相等,但是null和undefined确是相等的
//类型转换函数的使用,Number,String,Boolean,Object
</script>
</head>
<body>
</body>
</html>
javascript基础知识梳理-原始类型
最新推荐文章于 2014-05-13 17:13:26 发布