当前位置:网站首页>7. In JS [] = =! [] Why is it true?
7. In JS [] = =! [] Why is it true?
2022-07-26 14:55:00 【Song haha】
One : introduction
js It's a weakly typed language , Type conversion is the most basic .
Before I look at this question , Let's review js Type conversion rules :
Two : Judge true/false
stay js in ,`if` Can be judged true/false, by false The value is :
- false
- undefined
- null
- ''
- 0
- NaN
3、 ... and : Non operators
! You can convert the current value into boolean type
Four : operation
- +( The meaning of string splicing )
- -
- *
- /
1/'a'= NaN
// + As the meaning of splicing
1+true = 2 // true To 1 Go back and 1 Add up
1+null = 0 // null turn 0 Go back and 1 Add up
1+undefined = NaN // undefined Not numbers So the sum is NaN
1+{} = 1[object Object]
1 + +'123' = 124 // Execute first +'123' Convert it to a number And again 1 Add up
// Non digital addition
true+{} = true[object Object]
/**
reason :
There are two methods in the object valueOf toString
Will be adjusted first valueOf If valueOf The result is the raw data type , Then the original data type shall prevail ,
If it's a reference data type , Call toString()
console.log({}.valueOf().toString());[object Object]
step :
{}.valueOf() => {} .toString()=>[object Object]
*/5、 ... and : Compare
- <
- >
- ==
- ===
// console.log('a' < 'bbbb'); // true First to ascii Compare the code again
// console.log(1 < 'aaa'); // false If it can be converted into a string It can't be turned into numbers Just go back to false
// console.log(null == undefined); // true null and undefined The two equal signs are true however null and undefined Compare with other types All back to false
// console.log(null == 0); // false
// console.log(undefined == 0); // false
// console.log({} == {}); // false Compare memory addresses
// console.log(NaN == NaN); // false
// console.log('1' == 1); // true Will first string 1 Convert to numbers 1 Compare again
// console.log(1 == true); // true If it is boolean type Will also put boolean Turn it into numbers and compare
// console.log({} == '[object Object]'); // true object and character string Numbers symbol When comparing , Will convert the object to the original data type ( call toString Method ), Compare again
6、 ... and : Problem solving
console.log([] == ![]); // true
- First js Comparison The ternary operator has the highest priority Will execute first ![], [] The boolean type is true, This is OK , Add one in front ! That is to say false
- To the right of the equal sign is false, Boolean types do == operation It will be converted into numbers first Then compare That is, the right side of the equal sign is 0
- []==0 Operation time [] Would call valueof Get yourself [], Call again toString Method obtain ''
- ''==0 operation '' It's going to be 0 Compare again
- 0==0 So for true
边栏推荐
- Parsing XML files using Dom4j
- JS to realize the number to amount price thousand separator
- SA-Siam:用于实时目标跟踪的孪生网络
- Siamrpn++: evolution of deep network connected visual tracking
- WPF 常用功能整合
- 10 schemes to ensure interface data security
- OpenCV中图像算术操作与逻辑操作
- Wechat applet - "do you really understand the use of applet components?
- One stop monitoring of the software and hardware infrastructure of the whole university, and Suzhou University replaces PostgreSQL with time series database
- AMB | 迈向可持续农业:根际微生物工程
猜你喜欢

填问卷,领奖品 | 诚邀您填写 Google Play Academy 活动调研问卷

数据权限就该这么设计,yyyds!

Siamrpn++: evolution of deep network connected visual tracking

C common function integration

Win11 running virtual machine crashed? Solution to crash of VMware virtual machine running in win11
![[file upload vulnerability-06] distributed configuration file attack experiment - take upload-labs-4 as an example](/img/47/0e2a159bbbc7af8ef6263ca4ccc724.png)
[file upload vulnerability-06] distributed configuration file attack experiment - take upload-labs-4 as an example

Pdf translation, which translation company in Beijing is good

One stop monitoring of the software and hardware infrastructure of the whole university, and Suzhou University replaces PostgreSQL with time series database

【愚公系列】2022年7月 Go教学课程 017-分支结构之IF

GOM登录器配置免费版生成图文教程
随机推荐
2. 两数相加
Minecraft 1.16.5 module development (52) modify the original biological trophy (lot table)
Use of delve for go development and debugging
10 schemes to ensure interface data security
Fill in the questionnaire and receive the prize | we sincerely invite you to fill in the Google play academy activity survey questionnaire
[Nuxt 3] (十二) 项目目录结构 1
Leetcode summary
《MySQL高级篇》五、InnoDB数据存储结构
嵌入式开发:调试嵌入式软件的技巧
PyTorch中 nn.Conv2d与nn.ConvTranspose2d函数的用法
Error reported by Nacos enabled client
Embedded development: skills of debugging embedded software
NLP之NER:商品标题属性识别探索与实践
Would you please refer to the document of Database specification?
【1.2.投资的收益和风险】
Image-Level 弱监督图像语义分割汇总简析
BSN IPFS(星际文件系统)专网简介、功能、架构及特性、接入说明
网络图片转本地导致内核退出
C # use shift > > and operation and & to judge whether the two binary numbers have changed
Summary and analysis of image level weakly supervised image semantic segmentation