Skip to content
陈广亮的技术博客
Go back

null 与 undefined 的区别

定义

转义

!!null; // false
!!undefined; // false
Number(null); // 0
Number(undefined); // NaN
null == undefined; // true
null === undefined; // false

判定

var isNull = function (obj) { return obj === null; };
var isUndefined = function (obj) { return obj === void 0; };

undefined 的典型场景

  1. 变量声明未赋值
  2. 函数参数未提供
  3. 对象属性未赋值
  4. 函数无返回值
  5. 解构赋值时触发默认值(null 不会触发默认值)
const { a = "a", b = "b" } = { a: null };
// a → null, b → "b"

Share this post on:

Previous Post
实现链式函数 add
Next Post
ES6 Generator 与 co 模块