话不多说,直接先上结论: nullundefined 基本相同,只有细微差别

  • null 是表示缺少的标识,指示变量未指向任何对象,转为数值为 0
  • undefined 表示 “缺少值”,即该处应该有值,但还没有定义,转为数值为 NaN
//(1)null转为数值为0
console.log(Number(null));//0
console.log(null+3);//3
console.log(null == null);//true
console.log(null === null); //true
console.log(isNaN(null));false //(2)undefined转为数值为NaN
console.log(Number(undefined));//NaN(not a number)
console.log(3+undefined);//NaN
console.log(isNaN(undefined));//true //(3)null和undefined比较
console.log(null == undefined); //true,存在隐式类型转换
console.log(null === undefined);//false
/* "==="表示全等,二者类型不同,所以为false */
console.log(typeof null);//object数据类型
console.log(typeof undefined);//undefined数据类型

null 应用场景:

(1)作为函数的参数,表示该函数的参数不是对象

(2)作为对象原型链的终点

console.log(Object.getPrototypeOf(Object.prototype));//null

undefined 应用场景:

(1)一个没有被赋值的变量的类型是undefined

(2)调用函数时,应该提供的参数没有提供,该参数为undefined

(3)对象没有赋值的属性,该属性的值为undefined

(4)一个函数如果没有使用return语句指定返回值,就会返回undefined

//(1)变量未赋值
let a
console.log(a);//undefined //(2)函数参数未赋值
(function fn(x) {
console.log(x); //undefined
})() //(3)对象属性未赋值
let obj = {
name:'张三'
}
console.log(obj.name);
console.log(obj.age);//undefined //(4)函数没有返回值
function fn(){}
var x = fn();
console.log(x);//undefined

最新文章

  1. jQuery1.6源码分析系列
  2. 【转】如何配置android的adb环境变量
  3. npm配置文件
  4. 【GoLang】GoLang for 中有多个循环变量怎么处理?
  5. linux创建子进程--fork()方法
  6. laravel 指定 版本安装
  7. 学习angular 指令构造器时遇到的小问题
  8. 用bash命令得到Windows一个目录下的所有文件并且把结果输入到一个文件
  9. HDU 4746 Mophues 莫比乌斯反演
  10. c语言0 ‘0’ '\0'空格都是什么玩意儿
  11. PHP上传图片
  12. CreateFile,ReadFile等API详解(或者说MSDN的翻译)
  13. poi jsp xls
  14. Asp.net Core 跨域配置
  15. 真正理解拉格朗日乘子法和 KKT 条件
  16. MySql查询不区分大小写解决方案(两种)
  17. python学习---装饰器
  18. WPF-在Grid中编程添加控件
  19. thinkphp51初始化方法initialize
  20. (转)base64编码是怎么工作的?

热门文章

  1. ☕【Java技术指南】「TestNG专题」单元测试框架之TestNG使用教程指南(上)
  2. CPF 入门教程 - 各个控件介绍(八)
  3. 高德地图——2D转换3D
  4. Win7/Win10+VS2017+OpenCV3.4.2安装、测试
  5. PXE预启动执行环境
  6. Springboot_Email注解爆红
  7. golang GC 垃圾回收机制
  8. 前后端数据交互(四)——fetch 请求详解
  9. Linux下Sed替换时无法解析变量
  10. JDBC管理事务