当前位置:网站首页>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 + false
2.
**1.**
3.
[1, 2, 3] + [4, 5, 6]
4.
0.2 + 0.1 === 0.3
5.
10,2
6.
!!""
7.
+!![]
8.
true == "true"
9.
010 - 03
10.
"" - - ""
11.
null + 0
12.
0/0
13.
1/0 === 10 ** 1000
14.
true++
15.
"" - 1
16.
(null - 1) - "1"
17.
38 * 4343 * 2342+ (“true” — 0)
18.
5 + !5 + !!5
19.
[] + [1] + 2
20.
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 ==> 5
in 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
边栏推荐
- 微服务项目部署后,无法访问静态资源,无法访问到上传到upload中的文件,解决办法
- The forked VM terminated without saying properly goodbye
- 一网打尽异步神器CompletableFuture
- 强联通分量
- 周大福践行「百周年承诺」,真诚服务推动绿色环保
- The simplest way to open more functions without certificates
- 【学习笔记】阶段测试1
- R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用shadow_mark函数为动画添加静态散点图作为动画背景
- 关于memset赋值的探讨
- Faire un clip vidéo auto - média deux fois, comment clip n'est pas considéré comme une infraction
猜你喜欢
物联网应用技术专业是属于什么类
魅族新任董事长沈子瑜:创始人黄章先生将作为魅族科技产品战略顾问
Tdengine biweekly selection of community issues | phase III
Oneconnect listed in Hong Kong: with a market value of HK $6.3 billion, ye Wangchun said that he was honest and trustworthy, and long-term success
Mingfeng medical sprint technology innovation board: annual revenue of 350million yuan, proposed to raise 624million yuan
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
为什么我认识的机械工程师都抱怨工资低?
【学习笔记】阶段测试1
Thymeleaf th:with局部变量的使用
How to introduce devsecops into enterprises?
随机推荐
关于Apache Mesos的一些想法
What is the future development trend of neural network Internet of things
TiCDC 6.0原理之Sorter演进
Hongmeng fourth training
Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
Thymeleaf 常用函数
R语言使用ggplot2包的geom_histogram函数可视化直方图(histogram plot)
R language ggplot2 visual density map: Visual density map by group and custom configuration geom_ The alpha parameter in the density function sets the image transparency (to prevent multiple density c
How to call the function mode of one hand and one machine
freesurfer运行完recon-all怎么快速查看有没有报错?——核心命令tail重定向
ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
Interpretation of tiflash source code (IV) | design and implementation analysis of tiflash DDL module
PHP5下WSDL,SOAP调用实现过程
Financial one account Hong Kong listed: market value of 6.3 billion HK $Ye wangchun said to be Keeping true and true, long - term work
软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】
Some ideas about Apache mesos
根据CronSequenceGenerator计算cron表达式的时间
怎么叫一手一机的功能方式
总量分析 核算方法和势方法 - 分摊分析
3W principle [easy to understand]