博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript中的null和undefined有什么区别?
阅读量:2520 次
发布时间:2019-05-11

本文共 1109 字,大约阅读时间需要 3 分钟。

Let’s talk about the similarities first.

让我们先谈一下相似之处。

null and undefined are JavaScript primitive types.

nullundefined是JavaScript基本类型。

The meaning of undefined is to say that a variable has declared, but it has no value assigned.

undefined的意思是说已经声明了一个变量,但是没有赋值。

let age //age is undefined
let age = null //age is null

Note: accessing a variable that’s not been declared will raise a ReferenceError: <variable> is not defined error, but this does not mean it’s undefined.

注意:访问未声明的变量将引发ReferenceError: <variable> is not defined错误,但这并不意味着undefined

How do you check if a variable is null? Use the comparison operator, for example age === null

如何检查变量是否为空? 使用比较运算符,例如age === null

Same for undefined: age === undefined

与未定义相同: age === undefined

In both cases, you can check for:

在这两种情况下,您都可以检查:

if ( ! age ) {}

and this will be matching both null and undefined.

这将匹配nullundefined

You can also use the typeof operator:

您还可以使用typeof运算符:

let agetypeof age //'undefined'

although null is evaluated as an object, even though it is a primitive type:

尽管null被评估为一个对象,即使它是原始类型:

let age = nulltypeof age //'object'

翻译自:

转载地址:http://vmmgb.baihongyu.com/

你可能感兴趣的文章
c++ 继承产生的名字冲突问题 (1)
查看>>
SQL中on条件与where条件的区别
查看>>
Ubuntu下查看软件版本及安装位置
查看>>
安装IIS
查看>>
动态加载JS(转)
查看>>
SWUST OJ(961)
查看>>
js换空格为别的元素
查看>>
Recommendation Systems
查看>>
shell脚本 inotify + rsync 同步脚本
查看>>
maven pom 引入本地jar包
查看>>
QVT之The Relations Language(Part 二)
查看>>
python--dict和set类型--4
查看>>
快速实现Magento多语言的设置和产品数据的多语言方法
查看>>
python操作数据库
查看>>
Django的ORM基本操作补充一对多
查看>>
A - Oil Deposits(搜索)
查看>>
E - Phone List(字典序,string类型使用)
查看>>
自定义SeekBar三步
查看>>
"Coding Interview Guide" -- 设计一个有getMin功能的栈
查看>>
Java基础知识强化之多线程笔记06:Lock接口 (区别于Synchronized块)
查看>>