JavaScript基础---null和undefined

一、null

值 null 特指对象的值未设置。它是 JavaScript 基本类型 之一。值 null 是一个字面量,它不像undefined 是全局对象的一个属性。null 是表示缺少的标识,指示变量未指向任何对象。把 null 作为尚未创建的对象,也许更好理解

// foo现在已经是知存在的,但是它没有类型或者是值:
var foo = null; 
foo;
null

二、undefined

全局属性undefined表示原始值undefined,它是一个JavaScript的 原始数据类型。一个没有被赋值的变量的类型是undefined,一个函数如果没有使用return语句指定返回值,就会返回一个undefined值

function test(a){
    console.log(typeof a);    // undefined
    return a;
}

test();                       // 返回"undefined"

三、null与undefined的不同

typeof null        // object (因为一些以前的原因而不是'null')
typeof undefined   // "undefined"
null === undefined // false
null  == undefined // true
null === null // true
null == null // true
!null //true
isNaN(1 + null) // false
isNaN(1 + undefined) // true

猜你喜欢

转载自blog.csdn.net/qq_30817073/article/details/80653787