JavaScript 如何检查null值

JavaScript 如何检查null值

null值 表示任何对象值的缺失。通常会有意地将其设置为表示变量已声明但尚未被赋任何值。

这与类似的原始值 undefined 相反,后者是对象值的非意愿缺失。

这是因为声明但未赋任何值的变量是 undefined ,而不是 null

方法: 在JavaScript中有许多方法可以检查一个值是否为null:

  1. 使用相等运算符(===
  2. 使用Object.is()函数
  3. 使用typeof运算符

1. 使用相等运算符(===): 通过这个运算符,我们将学习如何通过(===)运算符检查JavaScript中的null值。该运算符只对null值通过,而不对undefined、false、0、NaN通过。

语法:

x === y;

示例: 以下代码片段展示了一些对象的比较。

JavaScript

<script> 
    const object1 = { 
        key: "value", 
    }; 
  
    const object2 = { 
        key: "value", 
    }; 
  
    console.log(object1 === object2); 
    console.log(object1 === object1);  
</script> 

输出:

false
true

2. 通过Object.is()函数: 该函数用于检查两个对象的值是否相等。如果两个值相同且都是null,那么这两个对象的值是相等的。

语法:

Object.is( a, null );

示例:

JavaScript

<script> 
    let maybeNull = null
  
    // The following is equivalent to  
    // maybeNull == null  
    // or maybeNull == undefined: 
    console.log(Object.is(maybeNull, undefined)  
        || Object.is(maybeNull, null)) 
          
    // Compare to the following: 
    console.log(maybeNull == null) 
    console.log(maybeNull == undefined) 
    console.log(maybeNull === null) 
    console.log(Object.is(maybeNull, null)) 
    console.log(maybeNull === undefined) 
    console.log(Object.is(maybeNull, undefined)) 
  
    maybeNull = undefined 
    console.log(maybeNull === undefined || maybeNull === null) 
    console.log(maybeNull == null) 
    console.log(maybeNull == undefined) 
    console.log(maybeNull === null) 
    console.log(Object.is(maybeNull, null)) 
    console.log(maybeNull === undefined) 
    console.log(Object.is(maybeNull, undefined))  
</script> 

输出:

true
true
true
true
true
false
false
true
true
true
false
false
true
true

3. 通过typeof操作符: 使用typeof操作符非常有用,因为如果一个变量没有声明,它将显示ReferenceError。但是,对于一个没有声明的值,typeof会返回undefined,因此使用typeof是检查null和undefined变量的更好方式。

语法:

typeof var;

示例:

JavaScript

<script> 
    // This will safely check a value to make sure it  
    // has been declared and assigned a value other 
    // than null or undefined: 
    console.log(typeof undeclaredVariable !== "undefined" 
        && (typeof undeclaredVariable !== "object" 
            || !undeclaredVariable)) 
  
    // Compare to checking for null using ==,  
    // which will only work for declared variables: 
    try { undeclaredVariable == null }  
    catch (e) { console.log(e) } 
    try { undeclaredVariable === null }  
    catch (e) { console.log(e) } 
    try { undeclaredVariable === undefined }  
    catch (e) { console.log(e) } 
  
    let declaredButUndefinedVariable 
    // Values that have been declared but not  
    // assigned a value will have the value  
    // undefined, which has a typeof undefined 
    console.log(typeof declaredButUndefinedVariable  
        !== "undefined" && 
        (typeof declaredButUndefinedVariable !== "object"
            || !declaredButUndefinedVariable)) 
  
    let stringVariable = "Here's Johnny!"
  
    // If the variable has been both declared and  
    // assigned a value that is neither null or undefined,  
    // we will get true. 
    console.log(typeof stringVariable !== "undefined" && 
        (typeof stringVariable !== "object" || !stringVariable)) 
  
    // This can be used to create a function that will return  
    // false for undefined, undeclared, and null values. 
    const isNotNullNorUndefined =  
        (value) => typeof value !== "undefined"
        && (typeof value !== "object" || !value) 
    console.log(isNotNullNorUndefined(stringVariable))  
</script> 

输出

...js:7:7)
    at Module._compile (node:internal/modules/cjs/loader:1155:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
    at Module.load (node:internal/modules/cjs/loader:1033:32)
    at Function.Module._load (node:internal/modules/cjs/loader:868:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:22:47
ReferenceError: undeclaredVariable is not defined
    at Object.<anonymous> (/home/guest/sandbox/55da4d93-5c09-4cd3-8be9-a1459e8ed1b0.js:8:7)
    at Module._compile (node:internal/modules/cjs/loader:1155:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
    at Module.load (node:internal/modules/cjs/loader:1033:32)
    at Function.Module._load (node:internal/modules/cjs/loader:868:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:22:47
false
true
true

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程