Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

javascript - JavaScript中的null和undefined有什么区别?(What is the difference between null and undefined in JavaScript?)

我想知道JavaScript中nullundefined之间的区别。

  ask by translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In JavaScript, undefined means a variable has been declared but has not yet been assigned a value, such as:

(在JavaScript中, undefined表示变量已被声明但尚未分配值,例如:)

var TestVar;
alert(TestVar); //shows undefined
alert(typeof TestVar); //shows undefined

null is an assignment value.

(null是赋值。)

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

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.

(从前面的示例中可以明显看出, undefinednull是两种不同的类型: undefined是类型本身(未定义),而null是对象。)

null === undefined // false
null == undefined // true
null === null // true

and

(和)

null = 'value' // ReferenceError
undefined = 'value' // 'value'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...