当前位置:网站首页>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
边栏推荐
- sp导出贴图到maya
- GOM登录器配置免费版生成图文教程
- PyTorch的简单实现
- 图神经网络Core数据集介绍
- Establishment of SSO single sign on environment based on CAS
- OSPF和MGRE实验
- Introduction to C language must brush the daily question of the collection of 100 questions (1-20)
- 次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
- LeetCode659.分割数组为连续子序列 (哈希表)
- 9、学习MySQL DELETE 语句
猜你喜欢
随机推荐
Create Yum warehouse inside the enterprise
1.两数之和
Matlab solution of [analysis of variance]
Create root permission virtual environment
Establishment of SSO single sign on environment based on CAS
OpenCV中图像算术操作与逻辑操作
Simple implementation of pytorch
[file upload vulnerability-06] distributed configuration file attack experiment - take upload-labs-4 as an example
[2022 national game simulation] Bai Loujian - Sam, rollback Mo team, second offline
JS wave animation effect menu style
过滤器和拦截器的区别
4 kinds of round head arrangement styles overlay styles
自编码器 AE(AutoEncoder)程序
SiamRPN++:深层网络连体视觉跟踪的演变
BSN IPFs (interstellar file system) private network introduction, functions, architecture and characteristics, access instructions
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
[dry goods] data structure and algorithm principle behind MySQL index
Win11运行虚拟机死机了?Win11运行VMware虚拟机崩溃的解决方法
CAS单点登录
Pdf translation, which translation company in Beijing is good









