js的undefined和null(面试高频考点)

363 阅读5分钟

1. undefined(js的原始数据类型,即方法或操作中变量创建(声明)未定义)

全局属性 undefined 表示原始值undefined。即方法或操作中创建(声明)未定义

  1. 一个没有被赋值的变量(的类型是 undefined。如果方法或者是语句中操作的变量没有被赋值,则会返回 undefined,

  2. 一个函数如果没有使用 return 语句指定返回值,就会返回一个 undefined 值。

  3. 一个函数没有传参,则默认传的参数是undefined

  4. 数组超出长度的获取的值-----undefined

  5. 访问对象中不存在的属性、方法-----undefined

  6. 你可以使用 undefined 和严格相等(===)或不相等(!==)操作符来决定一个变量是否拥有值(若变量未定义)

  7. Typeof 操作符和 undefined(使用 typeof的原因是它不会在一个变量没有被声明的时候抛出一个错误。)

  8. void操作符和undefined

  9. typeof(undefined)====undefined, typeof(null)===object

  10. Boolean(undefined/null/0/NAN/空串)===false, Boolean(对象)===true

  11. number(undefined)===NAN, number(null/false)===0

  12. String(undefined)===undefined, String(null)===null

  13. 字符串 + 其他类型 = 字符串

  14. undefined+数字+连接=NAN,undefined+Boolean+连接=NAN

  15. null+数字+连接=数字,

  16. Boolean+数字或其他类型+其他=1/0+数字或转换为Bollean做运算

  17. !(undefined/null/'')===true, !(其他类型)===false

  18. 如果想判断一个值是undefined、null和空字符串中的一种,只要用 ! 就可以,如果想判断一个值不是undefined、null和空字符串中的一种,则用 !! 就可以

       1.  一个没有被赋值的变量的类型是 undefined。
         如果方法或者是语句中操作的变量没有被赋值,则会返回 undefined,
         一个函数如果没有使用 return 语句指定返回值,就会返回一个 undefinedfunction test(a){
             console.log(typeof a);    // undefined
             return a;
         }
    
         test();
    
    
       2. 一个函数没有传参,则默认传的参数是undefined
            function test(t) {
               if (t === undefined) {
                 return 'Undefined value!';
               }
               return t;
             }
             let x;
             console.log(test(x));// expected output: "Undefined value!"
    
       3. 使用 undefined 和严格相等或不相等操作符来决定一个变量是否拥有值
             var x;
             if (x === undefined) {   //true
             // 执行这些语句
             } else {
             // 这些语句不会被执行
             }
     4. Typeof 操作符和 undefined
         var x;
         if(typeof x === 'undefined') {
             // 执行这些语句
         }
    
    
         使用 typeof的原因是它不会在一个变量没有被声明的时候抛出一个错误。没有声明技术方面看来这样的使用方法应该被避免
         // 这里没有声明 y
         if(typeof y === 'undefined') {       // 没有错误,执行结果为 true
            console.log("y is " + typeof y )  // y is undefined
         }
         if(y === undefined) {                // ReferenceError: y is not defined
    
         }
    
    
         一个变量是否被声明可以通过看它是否在一个封闭的上下文中被声明。唯一的例外是全局作用域,但是全局作用域是被绑定在全局对象上的,所以要检查一个变量是否在全局上下文中存在可以通过检查全局对象上是否存在这个属性(比如使用in操作符)
         if ('x' in window) {
           // 只有 x 被全局性的定义 才会执行这些语句
         }
    
     5. void操作符和undefinedvoid操作符是第三种可以替代的方法
         var x;
         if(x === void 0) {
             // 执行这些语句
         }
    
         // 没有声明 y
         if(y === void 0) {
             // 抛出一个 RenferenceError 错误 (与`typeof`相比)
         }
    
    
     6.警告:但是它有可能在非全局作用域中被当作标识符(变量名)来使用 (因为 undefined 不是一个保留字),这样做是一个非常坏的主意,因为这样会使你的代码难以去维护和排错。
     // 不要这样做!
     // 打印 'foo string' PS:说明 undefined 的值和类型都已经改变
     (function() {
     var undefined = 'foo';
     console.log(undefined, typeof undefined)
     })()
    
     // 打印 'foo string' PS:说明 undefined 的值和类型都已经改变
     (function(undefined) {
     console.log(undefined, typeof undefined)
     })('foo')
     
    

2. Null(js基本类型,是一个字面量,把 null 作为尚未创建的对象,特指对象的值未设置)

值 null 特指对象的值未设置,js本身不会给变量/对象的值设null,一些js原生方法会返回null。 值 null 是一个字面量,不像 undefined,它不是全局对象的一个属性。null 是表示缺少的标识,指示变量未指向任何对象。把 null 作为尚未创建的对象,也许更好理解。在 API 中,null 常在返回类型应是一个对象,但没有关联的值的地方使用。

  1. typeof(undefined)====undefined, typeof(null)===object

  2. Boolean(undefined/null/0/NAN/空串)===false, Boolean(对象)===true

  3. number(undefined)===NAN, number(null/false)===0

  4. String(undefined)===undefined, String(null)===null

  5. null === undefined // false, null == undefined // true,

  6. null === null // true, null == null // true, !null //true

  7. isNaN(1 + null) // false, isNaN(1 + undefined) // true

  8. !(undefined/null/'')===true, !(其他类型)===false

  9. 如果想判断一个值是undefined、null和空字符串中的一种,只要用 ! 就可以,如果想判断一个值不是undefined、null和空字符串中的一种,则用 !! 就可以

  10. 字符串+其他类型=字符串

  11. undefined+数字+连接=NAN, undefined+Boolean+连接=NAN

  12. null+数字+连接=数字,

  13. Boolean+数字或其他类型+其他= 1/0+数字或转换为Bollean做运算

  14. 对象引用错误-----返回null

  15. 一个对象不存在(如当访问一个不存的DOM节点时、Object的原型链终点时)----返回null

  16. null何时使用,当需要释放一个对象的时候可以将该对象赋值为null,进而来释放对象

    1.值 `null` 是一个字面量,不像 undefined,它不是全局对象的一个属性。`null` 是表示缺少的标识,指示变量未指向任何对象。把 `null` 作为尚未创建的对象,也许更好理解。在 API 中,`null` 常在返回类型应是一个对象,但没有关联的值的地方使用。
     // foo 不存在,它从来没有被定义过或者是初始化过:
     foo;   //"ReferenceError: foo is not defined"
    
     // foo 现在已经是知存在的,但是它没有类型或者是值:
     var foo = null;
     console.log(foo);   //null
     
     
     2. null何时使用,当需要释放一个对象的时候可以将该对象赋值为null,进而来释放对象
         var a={
             a:1,
             b:2
         };
         a=null;