JavaScript 中的 null 和 undefined 有什么区别? [英] What is the difference between null and undefined in JavaScript?
本文介绍了JavaScript 中的 null 和 undefined 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道 JavaScript 中的 null
和 undefined
有什么区别.
I want to know what the difference is between null
and undefined
in JavaScript.
推荐答案
在 JavaScript 中,undefined
表示变量已经声明但尚未赋值,例如:
In JavaScript, undefined
means a variable has been declared but has not yet been assigned a value, such as:
var testVar;
alert(testVar); //shows undefined
alert(typeof testVar); //shows undefined
null
是一个赋值.可以将其分配给变量作为无值的表示:
null
is an assignment value. It can be assigned to a variable as a representation of no value:
var testVar = null;
alert(testVar); //shows null
alert(typeof testVar); //shows object
从前面的例子中,很明显 undefined
和 null
是两种不同的类型: undefined
是一个类型本身(未定义)而null
是一个对象.
From the preceding examples, it is clear that undefined
and null
are two distinct types: undefined
is a type itself (undefined) while null
is an object.
null === undefined // false
null == undefined // true
null === null // true
和
null = 'value' // ReferenceError
undefined = 'value' // 'value'
这篇关于JavaScript 中的 null 和 undefined 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文