JavaScript undefined replaced with null

前端 未结 4 1246
后悔当初
后悔当初 2021-02-08 14:44

In JavaScript undefined can be reassigned, so it is often advised to create a self executing function that assures undefined is actually undefined. As an alternati

4条回答
  •  花落未央
    2021-02-08 15:14

    Because of this:

    var myVar1;
    var myVar2 = null;
    
    if (myVar1 === null) alert('myVar1 is null');
    if (myVar1 === undefined) alert('myVar1 is undefined');
    if (myVar2 === null) alert('myVar2 is null');
    if (myVar2 === undefined) alert('myVar2 is undefined');
    

    Anything set to null is not undefined - it's defined as null.

提交回复
热议问题