MUG*_*G4N 560 javascript null undefined
我知道,我知道必须有一些线索涵盖这个主题.但我使用搜索并没有得到符合我需要的答案.所以我们走了:
如何检查一个变量,如果是null或undefined,是什么之间的差异null和undefined?
"=="和"==="之间有什么区别(Google很难搜索到==)?
T.J*_*der 915
如何检查变量
null或者undefined......
是变量null:
if (a === null)
// or
if (a == null) // but see note below
Run Code Online (Sandbox Code Playgroud)
...但请注意,如果a是,后者也是如此undefined.
是吗undefined:
if (typeof a === "undefined")
// or
if (a === undefined)
// or
if (a == undefined) // but see note below
Run Code Online (Sandbox Code Playgroud)
......但是请注意,最后一个是模糊的; 如果a是的话也是如此null.
现在,尽管如此, 检查这些的常用方法是使用它们是假的事实:
if (!a) {
// `a` is falsey, which includes `undefined` and `null`
// (and `""`, and `0`, and `NaN`, and [of course] `false`)
}
Run Code Online (Sandbox Code Playgroud)
......是什么之间的差异
null和undefined?
它们都是通常用于表示缺少某些东西的值.undefined是更通用的,用作变量的默认值,直到它们被赋予一些其他值,作为调用函数时未提供的函数参数的值,以及当您询问对象时获得的值对于它没有的财产.但它也可以在所有这些情况下明确使用.(没有属性的对象与具有该值的属性undefined之间存在差异; undefined使用参数的值调用函数和完全保留该参数之间存在差异.)
null稍微具体一点undefined:它是一个空白对象参考.当然,JavaScript是松散类型的,但并非所有与JavaScript交互的东西都是松散类型的.如果像浏览器中的DOM这样的API需要一个空白的对象引用,我们null不会使用undefined.类似地,DOM的getElementById操作返回一个对象引用 - 一个有效的引用(如果它找到了DOM元素),或者null(如果它没有).
有趣的是(或不是),它们是他们自己的类型.也就是说,它null是Null类型undefined中唯一的值,并且是Undefined类型中唯一的值.
"=="和"==="之间的区别是什么?
它们之间的唯一区别是,它==会进行类型强制以尝试使值匹配,而===不会.所以例如"1" == 1是真的,因为"1"强迫1.但这"1" === 1是假的,因为类型不匹配.("1" !== 1是的.)第一个(实际)步骤===是"操作数的类型是否相同?" 如果答案是"否",结果是false.如果类型相同,那就完全可以==做到.
类型强制使用非常复杂的规则并且可以产生令人惊讶的结果(例如,"" == 0确实如此).
更多规格:
Jul*_*ier 92
差异很微妙.
在JavaScript中,undefined变量是一个从未声明过或从未赋值的变量.假设你说的var a;是,然后a将会undefined,因为它从未被赋予任何值.
但如果你再分配a = null;那么a现在就是null.在JavaScript中null是一个对象(typeof null如果您不相信我,请在JavaScript控制台中尝试),这意味着null是一个值(实际上甚至undefined是一个值).
例:
var a;
typeof a; # => "undefined"
a = null;
typeof null; # => "object"
Run Code Online (Sandbox Code Playgroud)
这可以证明在函数参数中很有用.您可能希望拥有默认值,但可以认为null是可接受的.在这种情况下,您可以:
function doSomething(first, second, optional) {
if (typeof optional === "undefined") {
optional = "three";
}
// do something
}
Run Code Online (Sandbox Code Playgroud)
如果省略optional参数doSomething(1, 2) thenoptional将是"three"字符串但如果你传递doSomething(1, 2, null)则可选null.
对于相等==且严格相等的===比较器,第一个是弱类型,而严格相等也检查值的类型.这意味着0 == "0"将返回true; while 0 === "0"会返回false,因为数字不是字符串.
您可以使用这些运营商之间要检查undefined的null.例如:
null === null # => true
undefined === undefined # => true
undefined === null # => false
undefined == null # => true
Run Code Online (Sandbox Code Playgroud)
最后一种情况很有意思,因为它允许您检查变量是未定义的还是null,而不是其他内容:
function test(val) {
return val == null;
}
test(null); # => true
test(undefined); # => true
Run Code Online (Sandbox Code Playgroud)
Tim*_*own 15
该规范是获得这些问题的完整答案的地方.这是一个总结:
x,您可以:null通过直接比较使用===.例:x === nullundefined采用两种基本方法:与undefined或直接比较typeof.出于各种原因,我更喜欢typeof x === "undefined".null并且undefined通过使用==和依赖于略微神秘的类型强制规则,这意味着x == null完全符合您的要求.==和之间的基本区别在于===,如果操作数具有不同的类型,===则将始终返回,false同时==将使用导致某些略微不直观的行为的规则将一个或两个操作数转换为相同的类型.如果操作数具有相同的类型(例如,两者都是字符串,例如在typeof上面的比较中),==并且===将表现完全相同.更多阅读:
未定义
这意味着该变量尚未初始化.
示例:
var x;
if(x){ //you can check like this
//code.
}
Run Code Online (Sandbox Code Playgroud)
等于(==)
它只检查值是否等于数据类型.
示例:
var x = true;
var y = new Boolean(true);
x == y ; //returns true
Run Code Online (Sandbox Code Playgroud)
因为它只检查值.
严格等于(===)
检查值和数据类型应该相同.
示例:
var x = true;
var y = new Boolean(true);
x===y; //returns false.
Run Code Online (Sandbox Code Playgroud)
因为它检查数据类型x是基本类型而y是布尔对象.
如何检查变量是否为null或未定义
只需检查变量是否具有如下有效值:
if(variable)
Run Code Online (Sandbox Code Playgroud)
如果变量不包含,它将返回true:
广告 1.null不是全局对象属性的标识符,例如undefined 可以
let x; // undefined
let y=null; // null
let z=3; // has value
// 'w' // is undeclared
if(!x) console.log('x is null or undefined');
if(!y) console.log('y is null or undefined');
if(!z) console.log('z is null or undefined');
try { if(w) 0 } catch(e) { console.log('w is undeclared') }
// typeof not throw exception for undelared variabels
if(typeof w === 'undefined') console.log('w is undefined');Run Code Online (Sandbox Code Playgroud)
广告 2.===检查值和类型。的==不要需要相同类型和由隐式转换比较之前(使用.valueOf()和.toString())。在这里你有所有(src):
如果
==(它的否定!=)
===(它的否定!==)
| 归档时间: |
|
| 查看次数: |
424052 次 |
| 最近记录: |