怎样理解undefined和 null

前言: undefined表示 "未定义", null 表示 "空"

第一步: 一般在变量或属性没有声明或者声明以后没有赋值时, 这个变量的值就是undefined;

typeof a; // "undefined";

var b;
typeof(b); // "undefined";

var c = {};
typeof(c.name); // "undefined";

第二步: null是检测变量指向的内存地址是否存在, 即: 如果变量不指向任何一个内存地址, 则返回null.

document.getElementById("fasdf")
// null
Object.prototype.__proto__;
// null

  

注意:

null 和 undefined 都是基本类型, 使用instanceof检测类型时都会返回false; 但null非常特殊, 使用typeof去判断时会返回object, 但是有instanceof判断时又会返回false; 一般判断某个变量是否为null, 使用的是: null === null;

null instanceof Object; // false
undefined instanceof Object; // false

typeof null; // "object";
typeof undefined; // "undefined";
原文地址:https://www.cnblogs.com/aisowe/p/11634836.html