JavaScript中的null和undefined有什么区别? [英] What is the difference between null and undefined in JavaScript?

查看:125
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆