前言

本期英文由@Dr. Axel Rauschmayer分享。

英文从这开始~~

Most programming languages have only one value for “no value” or “empty reference”. For example, that value is null in Java. JavaScript has two of those special values: undefined and null. They are basically the same (something that will change with ECMAScript 6, as will be explained in the last post of this series), but they are used slightly differently.

undefined is assigned via the language itself. Variables that have not been initialized yet have this value:

> var foo;

> foo

undefined

Similarly, JavaScript assigns undefined to missing parameters:

> function id(x) { return x }

> id()

undefined

null is used by programmers to explicitly indicate that a value is missing. E.g. for JSON.stringify():

> console.log(JSON.stringify({ first: 'Jane' }, null, 4))

{

"first": "Jane"

}

Check: does a variable have a value?

If you want to know whether a variable v has a value, you normally have to check for both undefined and null. Fortunately, both values are truthy. Thus, checking for truthiness via if performs both checks at the same time:

if (v) {

// v has a value

} else {

// v does not have a value

}

You’ll see more examples of the above check in the post for quirk 5 about parameter handling. There is one caveat: this check also interprets false, -0, +0, NaN and '' as “no value”. If that isn’t what you want then you can’t use it. You have two choices.

Some people advocate lenient non-equality (!=) to check that v is neither undefined nor null:

if (v != null) {

// v has a value

} else {

// v does not have a value

}

However, that requires you to know that != considers null to be only equal to itself and to undefined. I prefer the more descriptive use of !==:

if (v !== undefined && v !== null) {

// v has a value

} else {

// v does not have a value

}

Performance-wise, all three checks shown in this section are more or less the same. Hence, which one you will end up using depends on your needs and your taste. Some minification tools even rewrite the last check to a check via !=.

关于本文 作者:@Dr. Axel Rauschmayer 原文:https://2ality.com/2013/04/quirk-undefined.html

为你推荐

【第1244期】详解Object.create(null)

【英】在JavaScript中使用查询参数

null === undefined_【英】两个“非值”:undefined 和 null相关推荐

  1. JavaScript:undefined And null差异

    班吃饭的时候,同事偶然问了一个问题:undefined和null究竟有什么差别?无法回答,回去查阅相关文档,算了有了一个了解,做相关的总结.在開始之前,请看例如以下代码,算是抛出这个问题: conso ...

  2. java Null==undefined_javascript中的undefined和null有什么区别

    原标题:javascript中的undefined和null有什么区别 java中的undefined和null的区别有:类型不同前者返回的是未定义值后者是对象:转换原始类型方式不同前者是不支持转换后 ...

  3. Undefined、Null和NaN有什么区别?

    目录 数据类型比较 定义 nudefined null NaN 比较 数据类型比较 undefined和null为JS中的基本数据类型,NaN表示Not A Number! 看代码如下 console ...

  4. html js布尔值怎么定义,JavaScript基本类型值-Undefined、Null、Boolean

    大致介绍 ECMAScript中有5中简单的数据类型(也称为基本数据类型):Undefined.Null.Boolean.Number.String. Undefined Undefined时全局变量 ...

  5. mysql建表语句非空约束默认_Navicat mysql 建表字段 默认值 空白、NULL 、empty string的区别...

    总结在最后,没啥干货 新建一张用户表CREATE TABLE `user` ( `id` bigint(20) DEFAULT NULL COMMENT '编号', `name` varchar(64 ...

  6. javascript中not defined、undefined、null以及NaN的区别

    [ 前言 ] 从踏入IT行业开始,我就发现技术人员写博客很有必要.如果不写博客,时间久了就像好比天天在外面建房子的人,没有一个自己的房子一样.不管学习紧不紧,工作忙不忙,多动手进行梳理总结,一方面是为 ...

  7. c语言stdio中null的值,C/C++编程笔记:C语言NULL值和数字 0 值区别及NULL详解

    原创 程序员无言 2020-07-01 在学习C语言的时候,我们常常会碰到C语言NULL值和数字 0 ,很多小伙伴搞不清楚他们之间的一个区别,今天我们就了解一下他们之间的区别,一起来看看吧! 先看下面 ...

  8. Javascript数据类型:变量与数据类型 数字型Number 字符串型String 布尔型Boolean Undefined和Null 检测变量typeof 数据类型转换 标识符 关键字 保留字

    数据类型 一.数据类型意义 二.变量与数据类型 三.简单数据类型 1.数字型 Number 2.字符串型 String 3.布尔型 Boolean 4.未定义数据类型 Undefined 5.空值 N ...

  9. JS基础之undefined与null的区别

    在JavaScript开发中,被人问到:null与undefined到底有啥区别? 一时间不好回答,特别是undefined,因为这涉及到undefined的实现原理.于是,细想之后,写下本文,请各位 ...

最新文章

  1. Linux (CentOS)安装VNC+XFCE可视化桌面环境 附安装FireFox浏览器
  2. 解析SharedPreferences
  3. SpringBoot+Vue使用Get请求时提示:Error parsing HTTP request header
  4. jQuery-强大的jQuery选择器 (详解)
  5. python基础之删除文件及删除目录的方法
  6. dm9000 driver 1
  7. 计算机cpu温度显示原理图,腾讯计算机管家关于如何检查CPU温度的图形教程
  8. envoy重试_具有Envoy代理的微服务模式,第二部分:超时和重试
  9. android 商品筛选_商品关联分析
  10. python词云分析难吗_Python 词云分析周杰伦《晴天》
  11. React Native FlatList和SectionList列表组件
  12. php延迟静态绑定,延迟静态绑定——static
  13. My SQL外键约束
  14. RVCT31编译问题
  15. 数据挖掘和数据仓库之间的区别
  16. 零基础怎么自学日语?
  17. Sybase数据库知识总结
  18. 模拟电路——集成运算放大器(1)
  19. LNK1123转换到COFF期间失败
  20. word中插入分割线

热门文章

  1. Windows MinGW cmake 安装编译Opencv 3.4.3 C++开发环境
  2. python getopt参数参数自动补全_如何在Python中使用getopt / OPTARG?如果给出过多的参数(9),如何转移参数?...
  3. php搜索文件名,window_Windows7内置搜索如何同时搜索文件名与内容,  Win7的搜索功能效果非常强 - phpStudy...
  4. cmd文件 c语言的段,对于TMS320F2812的CMD文件的理解
  5. 如何为火狐浏览器添加附加组件?火狐浏览器附加组件管理器使用教程
  6. XSSFWorkbook与HSSFWorkbook的区别
  7. keyProperty=“id“ 和useGeneratedKeys=“true“作用
  8. String、StringBuilder、StringBuffer的区别
  9. java控制层创建websocket_用Java构建一个简单的WebSocket聊天室
  10. mysql取消主从配置_mysql主从配置