当前位置:网站首页>Share 20 strange JS expressions and see how many correct answers you can get
Share 20 strange JS expressions and see how many correct answers you can get
2022-07-05 14:19:00 【Front end talent】

JavaScript Is a very fault-tolerant programming language , Many expressions that are illegal in other programming languages are JavaScript Can work normally in .
This leads to a lot of strange code . Do you want to challenge it ?
Challenge
In this challenge , You will see 20 An odd expression , And guess the output .
1.
true + false2.
**1.**3.
[1, 2, 3] + [4, 5, 6]4.
0.2 + 0.1 === 0.35.
10,26.
!!""7.
+!![]8.
true == "true"9.
010 - 0310.
"" - - ""11.
null + 012.
0/013.
1/0 === 10 ** 100014.
true++15.
"" - 116.
(null - 1) - "1"17.
38 * 4343 * 2342+ (“true” — 0)18.
5 + !5 + !!519.
[] + [1] + 220.
1 + 2 + "3"Results and Analysis
true + false
Try to use the addition operator between two Boolean values (+) when , They will be converted into numbers .
And we all know that true Should be converted to 1,false Should be converted to 0. therefore true+false return 1.
[,,,].length
[,,,] Output an array with three empty slots . The last comma is the comma at the end .
You can think of it that way .
[,] ==> [empty,]
[,,] ==> [empty, empty,]
[,,,] ==> [empty, empty, empty,] therefore [,,,].length return 3.
[1, 2, 3] + [4, 5, 6]
When you try to use the addition operator between arrays (+) when , They will be converted to strings .
When converting an array to a string , Array of toString() Method is called .toString() The method is JavaScript For internal use , When an array needs to be displayed as text , It will connect its elements with commas .
[1, 2, 3].toString() ==> '1, 2, 3'
[4, 5, 6].toString() ==> '4, 5, 6'therefore
[1, 2, 3] + [4, 5, 6] ==> '1, 2, 3' + '4, 5, 6' ==> "1,2,34,5,6"0.2 + 0.1 === 0.3
Because floating point numbers are difficult to express accurately in computers , Mathematically 0.1 and 0.2 In the computer, only approximate numbers can be used to express .
0.1+0.2 The result is not entirely 0.3. not only JavaScript, Other programming languages have the same problem .
10, 2
comma (,) stay JavaScript Is also a legal operator , It evaluates each operand ( From left to right ), And return the value of the last operand .
therefore ,10,2 return 2
!!""
"" It's an empty string , It is an imaginary value .
Be careful :0、 An empty string ""、null and undefined They are all imaginary values .
! It's logical " Not " Operator , hold true become false, vice versa .
If we use it twice !, That is to say !!, It will convert a normal value into a Boolean value . therefore !"" return false.
+!![]
Arrays are all true values , Even empty arrays . therefore !![] Will return true.
!![]; // -> true and + No. will convert the truth value into its numerical representation : 1, therefore +!![] return 1.
true == "true"
Double equality operator (==) Check whether the two operands are equal , And return a boolean result .
According to the abstract double comparison rules , These two values are converted into numbers during comparison .
true == "true" ==> Number(true) == Number("true") ==> 1 == NaN therefore ,ture =="true" return false.
010 - 03
Here's a little trick : If a number with 0 start , So in JavaScript It is regarded as an octal digit . therefore :
010 - 03 ==> 8 - 3 ==> 5in addition :
If a number with 0b start , So it's in JavaScript Is regarded as binary digits .
If a number with 0x start , It's in JavaScript Is treated as a hexadecimal digit .

** ""--"" **
This seems to be a wrong syntax , But it does work .
Empty strings can be converted to Boolean values false Or numeric value 0. therefore -"" by 0

null + 0
As we said before ,null Is an imaginary value . It will be converted to Boolean false Or numeric value 0. So the result returns 0.
0/0
This is an illegal mathematical expression . equation 0/0 There is no meaningful numerical answer , The output is just NaN.
1/0 === 10 ** 1000
although 1/0 As before, it is also an illegal mathematical expression . But when the divisor is not 0 when ,JavaScript Think the result of this expression is Infinity.

and 10**1000 It's a big number ,JS This number cannot be represented correctly .(JavaScript The highest integer value in is 2^53-1). therefore 10 * 1000 It is also regarded as infinite (Infinity).
Infinity is always equal to another infinity , therefore 1/0 === 10 ** 1000 return true.
true++
This is nothing special , This is just a grammatical error .

""- 1
Although the addition operator (+) Used for both numbers and strings , But the subtraction operator (-) It's useless for strings , therefore JavaScript Interpret it as an operation between numbers . An empty string will be forced by the type 0.
"" - 1 ==> Number("") - 1 ==> 0 - 1 ==> -1 therefore "" — 1 return -1
** (null - 1) - "1" **
As mentioned above .
null ==> 0
(null - 1) ==> -1
"1" ==> 1 therefore (null — 1) — “1” return -2
38 * 4343 * 2342+ ("true" - 0)
You may wonder JS Is so crazy , So that it will string "true" Convert to Boolean true The number of represents . However , It's not that crazy . What actually happened was , It attempts to convert strings into numbers , But failed .
Number("true"); // -> NaN stay JavaScript In digital operation of , As long as one value is NaN, The final result of the operation must be NaN.38 * 4343 * 2342 Just a smoke bomb .
5 + !5 + !!5
As mentioned above .
0、 An empty string ""、null and undefined They are all imaginary values .
Non zero numbers are true values .
therefore :
!5 ==> 0
!!5 ==> 1**[] + [1] + 2 **
Trying to use the addition operator between arrays (+) when , They will be converted to strings .
[] ==> ''
[1] ==> '1'
[] + [1] ==> '1'
'1' + 2 ==> '12'So the result is '12'.

1 + 2 + "3"
JavaScript Perform these operations from left to right . When the number 3 And string 3 When adding , String connection will take precedence .
1 + 2; // -> 3
3 + "3"; // -> "33"summary
To be frank , These challenges do not provide any value for our coding skills , So you should not write this kind of code in actual projects
however , Use these skills as a pretense between friends and colleagues 13, Isn't it a very interesting thing ?
author :Marina Mosti translator : The front end little wisdom source :medium original text :https://medium.com/frontend-canteen/20-useless-but-funny-challange-for-javascript-develor-9eea39bb8efb
边栏推荐
- 关于memset赋值的探讨
- R语言ggplot2可视化条形图:通过双色渐变配色颜色主题可视化条形图、为每个条形添加标签文本(geom_text函数)
- freesurfer运行完recon-all怎么快速查看有没有报错?——核心命令tail重定向
- SAS接口有什么优势特点
- After the microservice project is deployed, static resources and files uploaded to upload cannot be accessed. Solution
- What are the advantages and characteristics of SAS interface
- R language uses the polR function of mass package to build an ordered multi classification logistic regression model, and uses the coef function to obtain the log odds ratio corresponding to each vari
- Guofu hydrogen energy rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, and 360million yuan of accounts receivable exceed the revenue
- WebRTC的学习(二)
- 3W principle [easy to understand]
猜你喜欢

Shen Ziyu, nouveau Président de Meizu: M. Huang Zhang, fondateur de Meizu, agira comme conseiller stratégique pour les produits scientifiques et technologiques de Meizu

Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading

How to deeply understand the design idea of "finite state machine"?

What are the advantages and characteristics of SAS interface

Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting

Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million

Thymeleaf 使用后台自定义工具类处理文本

Interpretation of tiflash source code (IV) | design and implementation analysis of tiflash DDL module

软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】

循环不变式
随机推荐
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the coef function to obtain the log odds ratio corresponding to eac
R language ggplot2 visualization: visual line graph, using legend in theme function The position parameter defines the position of the legend
世界环境日 | 周大福用心服务推动减碳环保
The forked VM terminated without saying properly goodbye
ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
根据CronSequenceGenerator计算cron表达式的时间
基于 TiDB 场景式技术架构过程 - 理论篇
判断变量是否为数组
Tdengine biweekly selection of community issues | phase III
Google eventbus usage details
Webrtc learning (II)
Scenario based technology architecture process based on tidb - Theory
How to introduce devsecops into enterprises?
Geom of R language using ggplot2 package_ Histogram function visual histogram (histogram plot)
【学习笔记】图的连通性与回路
The speed monitoring chip based on Bernoulli principle can be used for natural gas pipeline leakage detection
LeetCode_ 69 (square root of x)
鸿蒙第四次培训
周大福践行「百周年承诺」,真诚服务推动绿色环保
VC开发非MFC程序内存泄漏跟踪代码