当前位置:网站首页>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
边栏推荐
- go开发调试之Delve的使用
- [2022 national game simulation] Bai Loujian - Sam, rollback Mo team, second offline
- Seata deployment and microservice integration
- C语言入门必刷100题合集之每日一题(1-20)
- WPF 常用功能整合
- PyTorch中 nn.Conv2d与nn.ConvTranspose2d函数的用法
- 【无标题】
- Use of delve for go development and debugging
- 保证接口数据安全的10种方案
- What is the transport layer protocol tcp/udp???
猜你喜欢

When AI encounters life and health, Huawei cloud builds three bridges for them

Create Yum warehouse inside the enterprise

OpenCV中图像算术操作与逻辑操作
Network pictures are transferred locally, causing the kernel to exit

【无标题】

【常微分方程求解及绘图之求解小船行走轨迹】

目标跟踪相关知识总结

SiamFC:用于目标跟踪的全卷积孪生网络
![Matlab solution of [analysis of variance]](/img/30/638c4671c3e37b7ce999c6c98e3700.png)
Matlab solution of [analysis of variance]

Summary of target tracking related knowledge
随机推荐
[file upload vulnerability-06] distributed configuration file attack experiment - take upload-labs-4 as an example
1.两数之和
Stacked noise reducing auto encoder (sdae)
AMB | 迈向可持续农业:根际微生物工程
Maya imports the model into unity
[draw with toolbar]
【1.2.投资的收益和风险】
CVE-2022-33891漏洞复现
CVE-2022-33891 Apache spark shell 命令注入漏洞复现
Sqldeveloper tools quick start
Network pictures are transferred locally, causing the kernel to exit
LeetCode659.分割数组为连续子序列 (哈希表)
Fill in the questionnaire and receive the prize | we sincerely invite you to fill in the Google play academy activity survey questionnaire
Tdengine helps Siemens' lightweight digital solution simicas simplify data processing process
median filter
网络图片转本地导致内核退出
maya将模型导入到unity
Classic line style login interface
AMB | towards sustainable agriculture: rhizosphere microbial engineering
VBA upload pictures