Some minor knowledge about bugs in JavaScript programming, including javascriptbug

Source: Internet
Author: User

Some minor knowledge about bugs in JavaScript programming, including javascriptbug

JavaScript is one of the most popular programming languages today, but it is also a side effect of the language's various features, no matter how wonderful the language is, every day, thousands of programmers make a bunch of bugs. Do not laugh at others first. Maybe you are one of them.

Here is an example of a few simple and fully valid JS snippets (you can test it on your console ):
Copy codeThe Code is as follows:
Typeof NaN = 'number' // true
 
Infinity === 1/0 // true

0.1 + 0.2 = 0.3 // false, the same is true with parentheses.

"3" + 1 // '31'
"3"-1 // 2

Do you still trust your JavaScript?

1. Minimum JS Value

Copy codeThe Code is as follows:
Number. MIN_VALUE> 0; // true

Number. MIN_VALUE is the minimum value that can be expressed in JavaScript, Which is 5e-324. However, it is the Number closest to 0 in JS.

2. String connection

Copy codeThe Code is as follows:
("Foo" ++ "bar") = "fooNaN" // true
"Why I am" + typeof + "" // why I am number

Parse JS to "foo" + (+ "bar"), which converts "bar" into a number.

3. parseInt Function

Copy codeThe Code is as follows:
ParseInt ('06'); // 6
ParseInt ('08'); // 0 note that the new version of Google has been fixed
ParseInt (null, 24) === 23 // true

4. Is null an object?

Copy codeThe Code is as follows:
Typeof null // object
Null instanceof Object // false

5. return returned content

Copy codeThe Code is as follows:
Function myjson ()
{
Return
[
2
]
}

Myjson (); // undefined

The content returned by return must be in the same line as return.

6. Strange numbers

Copy codeThe Code 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
9999999999999999 // 10000000000000000
9999999999999999-1 // 10000000000000000
111111111111111111111 // 111111111111111110000

7. Weird Parameters

Copy codeThe Code is as follows:
Function hello (what ){
Alert (arguments [0]); // vicky
What = "world ";
Return "Hello," + arguments [0] + "! ";
}

Hello ("vicky"); // "Hello, world! "

8. A big equal sign

Copy codeThe Code 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.