Original link: https://jsisweird.com/ (recommend interested can do, experience the feeling of being abused, do not want to be abused continue to read 😄)

preface

JavaScript is a great conversion language, but because its original version was built in just ten days in 1995, plus JS backward compatibility, there are a lot of syntax that earthlings can’t understand 😒. It doesn’t always work the way you think it will. You’ll see 25 quirky expressions, most of which I don’t want you to use in everyday development, even if you’re a JS developer.

1. true + false

A. ‘truefalse’

B. 1

C. NaN

D. SyntaxError

Output: 1 ------ is a simple type conversion that does not translate Number(true); // -> 1 Number(false); // -> 0; / / - > 1Copy the code

2. [,,,].length

A. 0

B. 3

C. 4

D. SyntaxError

Output: 3 ------ [,,,] Outputs an array of three empty slots. The last comma is the trailing comma (sometimes called "final commas" is just the end). // -> "" [] + [] === [,] + [,]; // -> true [,,,] + [,,,]; // -> ",,,," ([,,,] + [,,,]).length === [,,,,].length; // -> trueCopy the code

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

A. ‘123456’

B. ‘1,2,34,5,6’

C. ‘6’

D. SyntaxError

Output: '1,2,34,5,6' ------ the answer is simple, just convert the array to a string and concatenate it. By the way, adding trailing commas doesn't change anything: [1, 2, 3,] + [4, 5, 6]; // -> "1,2,34,5,6" If you really wanted to convert arrays to comma-separated strings and combine them, you could write silly code like this: [1, 2,3] + [, 4,5,6]; / / - > "6" or, more stupid, this: [1, 2, 3, ""] + [4, 5, 6]. / / - > "6"Copy the code

4. 0.2 + 0.1 === 0.3

A. true

B. false

C. NaN

D. SyntaxError

Output: false ------ This is the dilemma of comparing floating point values. Instead of comparing two floating-point numbers directly, it is better to compare floating-point numbers with some degree of tolerance. 0.2 + 0.1; / / - > 0.30000000000000004; 0.2 + 0.1 > 0.3; // -> true (this is really stupid 😮)Copy the code

5. 10, 2

10.2 A.

B. 10

C. 2

D. 20

Output: 2 ------ The comma operator returns the last value 10, 2; // -> 2 1, 2, 3, 4; // -> 4 42, "pineapple", true; // -> trueCopy the code

6.!!!!!” “

A. true

B. false

C. undefined

D. SyntaxError

Output: false ------ You can precede any value with two exclamation marks to get its Boolean representation. In general, anything with a value is true and anything with a "null" value is false. Boolean(""); // -> false Boolean(0); // -> false Boolean("Pineapple"); // -> true Boolean(42); // -> trueCopy the code

7. +!!!!! []

A. true

B. false

C. 0

D. 1

Output: 1 ------ In the explanation above, it is mentioned that null values are usually represented by a Boolean value false. However, an empty array is an exception. It is denoted by true. The plus character then converts true to its numeric representation. Boolean([]); // -> true Number(true); / / - > 1Copy the code

8.!!!!!! true

A. true

B. false

C. 0

D. SyntaxError

Output: false ------ Placing three or more exclamation points in a row is very sandsculpted, so you may not have noticed before that you can even do this. However, since you've already written such sand-carving code, why not try more exclamation points hahaha!!!!!!!!!!!! true; // -> trueCopy the code

9. true == “true”

A. true

B. false

C. undefined

D. SyntaxError

Output: false ------ Both values are converted to numbers. Number(true); // -> 1 Number("true"); // -> NaN 1 == NaN; // -> falseCopy the code

The 010-03

A. 7

B. 5

C. 3

D. NaN

Output: 5 ------ 010 is considered an octal number by JavaScript. Therefore, its value is base 8. 010; / / - > 8 03. // -> 1; // -> 5 If you are willing to sand sculpture, you can go all out sand sculpture: 01111111111111111; // -> 40210710958665 by the way, the number of front zeros is irrelevant: 010 === 0000000010; // -> trueCopy the code

11. “” — “”

A. ”

B. 0

C. NaN

D. SyntaxError

Output: 0 ------ Both empty strings are converted to 0. Number(""); // -> 0 0 - - 0; // -> 0 The expression might be a little clearer if I write it like this: +"" -""; + 0-0; Note that although I put the space between the minus sign and the empty string just to mask you, the space between the minus sign itself is important: -- ""; / / - > 0 -- ""; // -> SyntaxErrorCopy the code

12. null + 0

A. 0

B. ‘null0’

C. NaN

D. TypeError

Output: 0 ------ Null converts to its numeric representation: 0. Number(null); // -> 0; // -> 0 this also means that although... null === false; // -> false ... This is true: +null === +false; // -> trueCopy the code

0/0 13.

A. 0

B. Infinity

C. NaN

D. SyntaxError

Output: NaN ------ Since equation 0/0 has no meaningful numeric answer, the Output is just NaN. isNaN(0/0); // -> trueCopy the code

14. 1/0 > Math.pow(10, 1000)

A. true

B. false

C. NaN

D. SyntaxError

Output: false ------ JavaScript treats both values as infinity, and infinity equals infinity. 1/0; // -> Infinity Math.pow(10, 1000); // -> Infinity Infinity > Infinity; // -> falseCopy the code

15. true++

A. 2

B. undefined

C. NaN

D. SyntaxError

Output: SyntaxError ------ first undefined++ does not cause SyntaxError: 1++; // -> SyntaxError "x"++; // -> SyntaxError undefined++; // -> NaN Of course, for complete clarity, this is valid syntax: let _true = true; _true++; _true; / / - > 2Copy the code

16. “” – 1

A. ‘1’

B. ‘-1’

C. -1

D. NaN

Output: -1 ------ The addition operator (+) is used for numbers and strings, and the subtraction operator (-) is not used for strings, so JavaScript interprets it as an operation between numbers. The empty string is converted to 0. Number(""); // -> 0 0-1; / / - > 1; This is true even if there are Spaces (or more) inside the string: "" -1; / / - > 1; However, if we use the addition operator, string concatenation takes precedence: "" + 1; / / - > "1";Copy the code

17. (null – 0) + “0”

A. ‘null0’

B. ’00’

C. 0

D. NaN

Output: '00' ------ Number(null) - 0; // -> 0; // -> "00" if only the subtraction operator was used, the result would be different: (null-0) - "0"; / / - > 0Copy the code

18. true + (“true” – 0)

A. 1

B. 2

C. NaN

D. SyntaxError

Output: NaN ------ Nothing to say, JS converts strings to numbers and fails. Number("true"); // -> NaNCopy the code

19. !5 + !5

A. 0

B. 10

C. 25

D. NaN

Output: 0 ------ All positive numbers are represented by the Boolean value true. The inverse of true is false, and false converts to 0. Boolean(5); // -> true ! true; // -> false Number(false); // -> 0; / / - > 0Copy the code

20. [] + []

A. []

B. [,]

C. ”

D. NaN

Output: "------ JavaScript converts the array to a string. [].toString(); // -> "" "" + ""; / / -- > "" because of the trailing comma, these expressions are equal: [] + [] = = = [and] + [and]; // -> true Although these arrays are different, they are all converted to empty strings: [].length; // -> 0 [,].length; // -> 1 [].toString() === [,].toString(); Number([]) === Number([,]); // -> trueCopy the code

21. NaN === NaN

A. true

B. false

C. TypeError

D. SyntaxError

Output: false ------ This is due to a decision made by the IEEE-754 Committee for a number of reasons/also, although NaN may not equal itself...... NaN === NaN; / / - > false... But these two statements are true. isNaN(NaN); // -> true Object.is(NaN, NaN); // -> trueCopy the code

22. NaN++

A. NaN

B. undefined

C. TypeError

D. SyntaxError

Output: NaN ------ Attempts to perform numeric operations on NaN, only NaN is Output. let _NaN = NaN; _NaN++; isNaN(_NaN); // -> true _NaN--; isNaN(_NaN); // -> true _NaN *= 10; isNaN(_NaN); // -> trueCopy the code

23. undefined + false

A. ‘undefinedfalse’

B. 0

C. NaN

D. SyntaxError

0utPUT: NaN ------ Although false can be converted to a number, undefined cannot. Number(false); // -> 0 Number(undefined); // -> NaN NaN + 0; // -> NaN however, undefined can be converted to false:!! undefined === false; // -> true this means that we can add undefined and false: undefined + false; / / - > 0Copy the code

24. + 0 = = = 0

A. true

B. false

C. TypeError

D. SyntaxError

Output: true ------ Positive and negative zeros are equal in JavaScript. Interestingly, the object. is function disagrees. This is one of several cases where === does not return the same result as object. is. Object.is(0, -0); // -> falseCopy the code

25. – “” + + “1” * null – [,]

A. 0

B. ‘0’

C. NaN

D. i give up

Output: 0 ------ Ending Summary of most of the weird grammar covered in this quiz. Let's break it down piece by piece: -""; / / -- > 0 + "1"; // -> 1 Number(null); // -> 0 Number([,]); // -> 0 add them together: -0 + 1 * 0-0; / / - > 0Copy the code