当前位置:网站首页>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
边栏推荐
- codeforces每日5题(均1600)-第二十八天
- R语言计算时间序列数据的逐次差分(successive differences):使用diff函数计算时间序列数据的逐次差分值
- 自定义组件,并在组件中注入自定义组件实现多种场景的下的组件切换
- arm交叉编译
- 机器学习(十四):K均值聚类(kmeans)
- shell脚本详解-------循环语句wuile循环和until循环
- php如何查询字符串以什么开头
- 动态数组底层是如何实现的
- Copycat CNN: Stealing Knowledge by Persuading Confession with Random Non-Labeled Data阅读心得
- R语言缺失时间序列的填充及合并:补齐时间序列数据中所有缺失的时间索引、使用merge函数合并日期补齐之后的时间序列数据和另外一个时间序列数据(补齐左侧数据)
猜你喜欢
学习探索-网站中引入百度统计
力拓信创生态,博睿数据多款产品获得东方通与达梦数据库产品兼容互认证明
shell脚本详解 --------循环语句之for循环
荣耀发布开发者服务平台,智慧生态合作提速
对象实例化之后一定会存放在堆内存中?
The second step through MySQL in four steps: MySQL index learning
Understand Chisel language. 32. Chisel advanced hardware generator (1) - parameterization in Chisel
LeetCode Question of the Day - 1403. Minimum Subsequence in Non-Increasing Order
安装win11提示开启安全模式如何解决
shell脚本详解-------循环语句wuile循环和until循环
随机推荐
RecyclerView 缓存与复用机制
Unity Apple登录接入
NLP未来,路在何方?从学术前沿和业界热点谈起
面试官:可以谈谈乐观锁和悲观锁吗
SRM Supplier Collaborative Management System Function Introduction
Learning and Exploration-Introducing Baidu Statistics to the Website
启动项目(瑞吉外卖)
正则过滤字符串中 script 标签
GraphQL 入门与实践
WEB 渗透之逻辑漏洞
el-date-picker 设置时间范围
字节二面被问到mysql事务与锁问题,我蚌埠住了
机器学习(十九):梯度提升回归(GBR)
SAP ABAP SteammPunk 蒸汽朋克的最新进展 - 嵌入式蒸汽朋克
How to convert an int attribute into a string in the json format returned by the Go language gin framework?
R语言dplyr包group_by函数和summarise_at函数计算dataframe计算不同分组的计数个数和均值、使用%>%符号将多个函数串起来
(1), the sequential storage structure of linear table chain storage structure
提高图片清晰度的快速方法?
抖音最重要的接口——item_search_video-根据关键词获取视频列表
Codeforces Round #811 (Div. 3)