search
HomeWeb Front-endJS Tutorial20 weird JS expressions, guess the output results!

This article will share with you 20 weird JavaScript expressions. Can you answer their output results? Come and answer the challenge!

20 weird JS expressions, guess the output results!

#JavaScript is a very fault-tolerant programming language, and many expressions that are illegal in other programming languages ​​will work fine in JavaScript.

This leads to a lot of weird code. Do you want to challenge it?

Challenge

In this challenge, you will see 20 weird expressions and have to guess their 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

When trying to use the addition operator ( ) between two Boolean values , they will be converted to numbers.

And we all know that true should be converted to 1 and false should be converted to 0. So true false returns 1.

[,,,].length

[,,,] Outputs an array with three empty slots. The last comma is the trailing comma.

You can think of it this way.

[,]     ==> [empty,]
[,,]    ==> [empty, empty,]
[,,,]   ==> [empty, empty, empty,]

So [,,,].length returns 3.

[1, 2, 3] [4, 5, 6]

When you try to use the addition operator ( ) between arrays, they are Convert to string.

When converting an array to a string, the array's toString() method is called. toString()The method is used internally by JavaScript. 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'

So

[1, 2, 3] + [4, 5, 6] ==> '1, 2, 3' + '4, 5, 6' ==> "1,2,34,5,6"

0.2 0.1 === 0.3

Since floating point numbers are difficult to accurately represent in computers, mathematical 0.1 and 0.2 can only be represented by approximate numbers in computers. The result of

0.1 0.2 is not exactly 0.3. Not just JavaScript, other programming languages ​​have the same problem.

10, 2

The comma (,) is also a legal operator in JavaScript, it evaluates each operand (from left to right to the right) and returns the value of the last operand.

Therefore, 10,2 returns 2

!!""

"" is an empty string, it is a false value.

Note: 0, empty string "", null and undefined are all virtual values.

! is the logical "not" operator, turning true into false and vice versa.

If we use ! twice, which is !!, it will convert a normal value into a boolean value. So !"" returns false.

!![]

Arrays are all true values, even empty arrays. So !![] will return true.

!![]; // -> true

and will convert the true value to its numeric representation: 1, so !![] returns 1.

true == "true"

The double equality operator (==) checks whether its two operands are equal and returns a Boolean result.

According to the abstract equality comparison rules, both values ​​are converted to numbers when compared.

true == "true" ==> Number(true) == Number("true") ==> 1 == NaN

So, ture == "true" Return false.

010 - 03

Here’s a little trick: if a number starts with 0, it is treated as Make an octal number. So:

010 - 03 ==> 8 - 3 ==> 5

Also:

  • If a number starts with 0b, then it is treated as a binary number in JavaScript.
  • If a number starts with 0x, it is treated as a hexadecimal number in JavaScript.

20 weird JS expressions, guess the output results!

""--""

This looks like a bad syntax , but it does work fine.

The empty string can be converted to the Boolean value false or the numeric value 0. So -"" is 0

20 weird JS expressions, guess the output results!

##null 0

正如我们之前所说,null是一个虚值。它将被转换为布尔值false或数字值0。所以结果返回 0

0/0

这是一个非法的数学表达式。方程0/0没有任何有意义的数字答案,输出的结果只是NaN

1/0 === 10 1000**

虽然1/0和之前一样也是一个非法的数学表达式。但是当除数不是0时,JavaScript认为这个表达式的结果是Infinity

20 weird JS expressions, guess the output results!

10**1000是一个很大数字,JS 无法正确表示这个数字。(JavaScript中最高的整数值是2^53-1)。所以10 * 1000也被当作无限大(Infinity)。

无穷大总是等于另一个无穷大,所以1/0 === 10 ** 1000返回 true。

true++

这没有什么特别的,这只是一个语法错误。

20 weird JS expressions, guess the output results!

""- 1

虽然加法运算符(+)同时用于数字和字符串,但减法运算符(-)对字符串没有用处,所以JavaScript将其解释为数字之间的操作。一个空的字符串会被类型强制为0。

"" - 1 ==> Number("") - 1 ==> 0 - 1 ==> -1

所以 "" — 1 返回 -1

(null - 1) - "1"

正如上面所说。

null ==>  0
(null - 1) ==> -1
"1" ==> 1

所以 (null — 1) — “1” 返回 -2

38 4343 2342+ ("true" - 0)

你可能会怀疑JS是如此疯狂,以至于它将字符串 "true" 转换为布尔值 true 的数字表示。然而,它并没有那么疯狂。实际发生的情况是,它试图将字符串转换为数字,但失败了。

Number("true"); // -> NaN

在JavaScript的数字运算中,只要有一个值是NaN,运算的最终结果就一定是NaN。38 * 4343 * 2342只是一个烟雾弹。

5 + !5 + !!5

正如上面所说。

  • 0、空字符串""、null和undefined都是虚值。
  • 非零的数字是真值。

所以:

!5 ==> 0
!!5 ==> 1

[] + [1] + 2

试图在数组之间使用加法运算符(+)时,它们会被转换为字符串。

[] ==> ''
[1] ==> '1'
[] + [1] ==> '1'
'1' + 2 ==> '12'

所以结果是'12'。

20 weird JS expressions, guess the output results!

1 + 2 + "3"

JavaScript 从左到右执行这些操作。当数字3与字符串3相加时,字符串连接将优先进行。

1 + 2; // -> 3
3 + "3"; // -> "33"

总结

坦率地说,这些挑战并没有为我胶们编码技能提供任何价值,所以不应该在实际项目中写这种代码

但是,把这些技巧作为朋友和同事之间的一些装13,不是一件非常有趣的事情吗?

作者:Marina Mosti

来源:medium

原文:https://medium.com/frontend-canteen/20-useless-but-funny-challange-for-javascript-develor-9eea39bb8efb

【相关视频教程推荐:web前端

The above is the detailed content of 20 weird JS expressions, guess the output results!. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment