当前位置:网站首页>JS中null与undefined的异同点
JS中null与undefined的异同点
2022-08-04 17:18:00 【ttyt1217】
整理了网上资料【※注1】,结合起来理解比较好:
一、 相同点
·if 判断语句中,两者都会被转换为false
·二者值判断(==)结果相等
null == undefined//true
二、不同点
·Number转换的值不同,
Number(null) 输出为 0,
Number(undefined)输出为 NaN。
·1、null
null的类型是Object,表示"没有对象",即该处不应该有值。
典型用法是:
(1) 作为函数的参数,表示该函数的参数不是对象。
(2) 作为对象原型链的终点【※注2】
Object.getPrototypeOf(Object.prototype)
(3) 清空对象直接赋值为null,而不是用delete
(4) 定义一个值为null是合理的,但定义为undefined不合理(var name = null)
·2、undefined
undefined的类型为Undefined,表示"缺少值",就是此处应该有一个值,但是还没有定义。
典型用法是:
(1)变量被声明了,但没有赋值时,就等于undefined。
var i;
i // undefined
(2) 调用函数时,应该提供的参数没有提供,该参数等于undefined。
function f(x){console.log(x)}
f() // undefined
(3)对象没有赋值的属性,该属性的值为undefined。
var o = new Object();
o.p // undefined
(4)函数没有返回值时,默认返回undefined。
var x = f();
x // undefined
二者类型区别:
null == undefined//true
null === undefined //false
typeof null == typeof undefined//false
typeof undefined === 'undefined'//true
typeof null === 'object'//true
【※注1】:
https://blog.csdn.net/qq_38921817/article/details/77145759
https://www.mk2048.com/blog/blog_hjbh220h10bb.html
https://blog.csdn.net/wuyitong116/article/details/90488044
https://blog.csdn.net/weixin_39713762/article/details/93807832
【※注2】:关于 原型链 的定义,参见:
js(原型对象与原型链)
https://www.cnblogs.com/lyx1014/p/11040462.html
JS高级---原型链(一看就懂,但18岁以下请绕道)
https://blog.csdn.net/xiaotao_css/article/details/72782416
边栏推荐
猜你喜欢

Nacos集群搭建

Digital-intelligent supply chain management system for chemical manufacturing industry: build a smart supply system and empower enterprises to improve production efficiency

Clearance sword refers to Offer——The sword refers to Offer II 010. and the sub-array of k

学习探索-给字体设置前景色

HCIP WPN 实验

通关剑指 Offer——剑指 Offer II 010. 和为 k 的子数组

要有遥不可及的梦想,也要有脚踏实地的本事

15 days to upgrade to fight monsters and become a virtual fashion creator

88.(cesium之家)cesium聚合图

全世界国家和地区国家顶级域名对照表
随机推荐
水能自发变成“消毒水”,83岁斯坦福教授:揭示冬天容易得流感的部分原因...
JSP的Web监听器(Listener)
】 【 LeetCode daily one problem - 540. The order of a single element of the array
Flutter实战-请求封装(四)之gzip报文压缩
Understand Chisel language. 32. Chisel advanced hardware generator (1) - parameterization in Chisel
从云计算到函数计算
并发编程原理学习-reentrantlock源码分析
GraphQL 入门与实践
response的contentType 几种类型
安装win11提示开启安全模式如何解决
codeforces每日5题(均1600)-第二十八天
学习探索-给字体设置前景色
php如何查询字符串以什么开头
R语言时间序列数据算术运算:使用diff函数计算时间序列数据的逐次差分、使用时间序列之间的除法计算相对变化率(乘以100获得百分比)
机器学习(十九):梯度提升回归(GBR)
Boost library study notes (1) Installation and configuration
Qt自动补全之QCompleter使用
C# Sqlite database construction and use skills
华硕win11安全启动如何开启
北京海淀6家必胜客被暂停外卖订餐 存在食品安全问题