你所不知道的 JS: null , undefined, NaN, true==1=="1",false==0=="",null== undefined
1
1
1
=== 全相等(全部相等)
== 值相等(部分相等)
demo:
var x=0; undefined var y=false; undefined if(x===y){ console.log("all equal!") }else if(x==y){ console.log("just value equal!") }else{ onsole.log("not at all equal!") } VM3163:1 just value equal!
1
null == undefined
true == 1 == "1"
false == 0 == ""
"" + ? == "?" (anything)
null (value) == 0 (仅当 null + 数字时,成立!)
false+0;// false==0
0
true+0;// true==1
10+undefined;
NaN
0+null;// null (value)==0; null==0; false
0
0+NaN;
NaN1+undefined;
NaN
1+null;// null (value)==1; null==0; false
1
1+NaN;
NaN
NaN+"";
"NaN"
null+"";
"null"
undefined+"";
"undefined"true+"";
"true"
false+"";
"false"
typeof null;
"object"typeof NaN;
"number"typeof undefined;
"undefined"typeof false;
"boolean"typeof true;
"boolean"typeof "";
"string"typeof 0;
"number"
1
1
1
1
1
x
undefined==null; true NaN==null; false NaN==undefined; false
<script> var a; if(a==undefined) alert('undefined');//true if(a==null) alert('null');//true if(null==NaN) alert('NaN=null');//undefined if(undefined==NaN) alert('NaN=undefined');//undefined if(NaN) alert('NaN');//undefined </script>null==0; false null===0; false undefined==0; false undefined===0; false null==undefined; true null===undefined; false <script> var a=null;//(0/false) var b=undefined;//(false) var c = 7 ; alert('undefined+null='+(a+b));//NaN alert('c+null='+(a+c));//7 alert('c+undefined='+(b+c));//NaN </script> chrome 测试结果! 个人认为: null 的取值是(0/false), undefined 的取值是(false/NaN)。null==0; false null===0; false undefined undefined undefined==0; false undefined===0; false null==false; false undefined==false; false
NaN==0; false NaN==false; false typeof NaN; "number" NaN==undefined; false typeof null; "object" typeof undefined; "undefined"
false == 0, true == 1
null+false; 0 undefined+false; NaN NaN+false; NaN NaN+null; NaN false+false; 0 false+true; 1 true+true; 2 null+true; 1 undefined+true; NaN NaN+true; NaNfalse==0; true false===0; false true==1; true true===1; false0==false; true 0===false; false 1==true; true 1===true; false
1
1
//前端JS blogs: <!-- http://www.ruanyifeng.com/blog/2014/03/undefined-vs-null.html --> <!-- http://yanhaijing.com/javascript/2014/01/05/exploring-the-abyss-of-null-and-undefined-in-javascript/ --> //前端JS试题: <!-- https://github.com/N-ZOO/everycode/issues/13 --><script> var a; if(a==undefined) alert('undefined');//true if(a==null) alert('null');//true if(null==undefined) alert('null==undefined');//true if(undefined==null) alert('undefined==null');//true var b =5 ; alert('b+undefined=||b+null='+(a+b));//NaN </script> <script> var a = null;//(0/false) var b = undefined;//(false) var c = 7; var x; alert('undefined+null='+(a+b));//NaN alert('c+null='+(a+c));//7 alert('c+undefined='+(b+c));//NaN if(x==undefined) alert('undefined');//true if(x==null) alert('null');//true if(null==NaN) alert('NaN=null');//undefined if(undefined==NaN) alert('NaN=undefined');//undefined if(NaN) alert('NaN');//undefined </script>
1
1
1
1
1
1
1
1
=== 与 == 区别?
使用 === 判断 两个变量是否完全相等
== 可能会导致,判断错误[code=javascript]//使用 === 判断 两个变量是否完全相等 //== 可能会导致,判断错误 (undefined == 0) //false (null == 0) //false (null+1) //1 (undefined+1) //NaN (undefined===null) //false (undefined==null) //true
1
1
1
var a; undefined a==null; true a==undefined; true a===null; false a===undefined; true a=0; 0 a===undefined; false a==undefined; false a===null; false a==null; false a 0 a+null; 0 a+undefined; NaN a+NaN; NaN a 1 a+NaN; NaN a+undefined; NaN a+null; 1 null==0; false null===0; false undefined==0; false undefined===0; false null==undefined; true null===undefined; false
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/5769202.html
未经授权禁止转载,违者必究!