当前位置:网站首页>js基础及常考面试题之 [] == ![]结果为true, []==[]结果为false 详解
js基础及常考面试题之 [] == ![]结果为true, []==[]结果为false 详解
2022-06-10 19:32:00 【Rulyc】
[] == ![]
第一步: !优先级高于==,故而先计算 ![]; 所有的对象都是true,所以 []为true; 所以!true就为false
即: [] == false
第二步:如果有一个操作数为布尔类型,则将布尔类型转化为数值类型
即: [] == 0
第三步: 将另一个操作类型转化为数值类型,[]转化为数值类型为0
即 0 == 0
故: 结果为 true
[] == []
左右两边类型相同,都是数组类型;但,数组或者对象在比较的时候,是比较他们的引用地址
如: var a = []; var b=a; console.log(a==b) -----------true
原因: a与b都是引用的同一个地址,a与b的随意一个改变,都会引起地址变化.
如: var a= [], b = [] ; console.log(a==b)--------------false
原因: a与b的地址不同
形象例子: 就好比[] 是一笼包子
第一种场景: a买了一笼包子, 并把这一笼子共享给了b, a与b也就共同拥有这一笼包子,谁吃都会减少
第二种场景: a买了一笼子,b也买了一笼子, a吃一个包子,b的那笼包子里并不会减少。
故而: 结果为false
附上==, === 的流程图
在 JS 中类型转换只有三种情况,分别是:
转换为布尔值
转换为数字
转换为字符串
js中数值转化表格:
边栏推荐
猜你喜欢

It took 2 years, 442 authors and 132 institutions! Google released the new benchmark big bench for language model evaluation. 204 tasks comprehensively evaluated the language model ability, with paper

Microsoft Word tutorial "5", how to change the margins and create a newsletter column in word?

Knowledge map / relationship visualization

An old programmer of about 10 years said: simple crud function enters the era of codeless development 1. Adding, deleting, modifying and checking interface information

vulnhub-The Planets: Earth

服务管理与通信,基础原理分析

SBC芯片35584数据手册预调节器翻译

Hm3416h buck IC chip pwm/pfm controls DC-DC buck converter

I drew several exquisite charts with plotly, which turned out to be beautiful!!

【技术碎片】重名文件 加后缀重命名过滤实现
随机推荐
【技术碎片】重名文件 加后缀重命名过滤实现
2022 Devops roadmap | medium
ESP8266 系统环境搭建
An old programmer of about 10 years said: simple crud function enters the era of codeless development 1. Adding, deleting, modifying and checking interface information
Microsoft Word 教程「5」,如何在 Word 中更改页边距、创建新闻稿栏?
canvas 高级功能(中)
FPGA state machine
SBC芯片35584数据手册预调节器翻译
Qualcomm qc2.0 fast charging intelligent identification IC fp6719
Rotated Sorted Array旋转排序数组相关题
刷脸认证如何实现人脸又快又准完成校验?
mysql服务启动失败
Recording a super Oolong mental retardation bug may help people like me eat for free
[technical fragment] implementation of renaming and filtering duplicate name files with suffixes
How to realize face verification quickly and accurately?
Microsoft Word 教程「5」,如何在 Word 中更改頁邊距、創建新聞稿欄?
深度学习调参经验和工具
nodejs: 官方文档3 dgram Stream
补水仪108K加湿器开发方案_单片机_NY8A051F_单片机开发设计开发
为什么网页样式属性,有的需要加冒号“:”,有的不用?