JavaScript is one of the most popular programming languages today, but popularity is also the side effects of the language's own characteristics. No matter how wonderful the language is, thousands of programmers still make a bunch of bugs every day. Don't laugh at others for now, maybe you are one of them.
Here are a few short, fully valid JS snippets (you can experiment with this on your console):
The code copy is as follows:
typeof NaN === 'number' // true
Infinity === 1/0 // true
0.1 + 0.2 === 0.3 // false, the same is true for prefixed parentheses
"3" + 1 // '31'
"3" - 1 // 2
Do you still believe in your JavaScript?
1. The minimum value of JS
The code copy is as follows:
Number.MIN_VALUE > 0; //true
Number.MIN_VALUE is used for the minimum value that JavaScript can express, which is 5e-324, but it is the closest number in JS to 0.
2. String connection
The code copy is as follows:
("foo" + + "bar") === "fooNaN" //true
"why I am " + typeof + "" // why I am number
JS parses into "foo" + (+ "bar"), which will convert "bar" into a number
3. parseInt function
The code copy is as follows:
parseInt('06'); // 6
parseInt('08'); // 0 Note that Google's new version has been fixed
parseInt(null, 24) === 23 // true
4. Is null object?
The code copy is as follows:
typeof null // object
null instanceof Object // false
5. Return Return to content
The code copy is as follows:
function myjson()
{
Return
[
2
]
}
myjson(); // undefined
The content returned by return must be on the same line as return
6. Strange numbers
The code copy is as follows:
012 == 12 // false
'012' == 12 // true
"3" + 1 // '31'
"3" - 1 // 2
0.1 + 0.2 == 0.3 // false
0.1 + 0.7 == 0.8 // false
0.2 + 0.7 == 0.9 // false
9999999999999999999 // 10000000000000000000
99999999999999999999-1 //10000000000000000000
1111111111111111111111111 // 111111111111111111110000
7. Strange parameters
The code copy is as follows:
function hello(what) {
alert(arguments[0]); //vicky
what = "world";
return "Hello, " + arguments[0] + "!";
}
hello("vicky"); //"Hello, world!"
8. The equal sign that makes people lose their heads
The code copy is as follows:
NaN === NaN; // false
[] == false; // true
"" == false; // true
null == false; // false
[] == ![] // true
window.window == window // true
window.window === window // false, some browsers are true
window == document // true, some browsers are false
("0" && {}) == 0 // false
(0 && {}) == 0 // true
0 == "0" // true
[] == 0 // true