Tips for bugs in JavaScript programming _ javascript skills

Source: Internet
Author: User
This article mainly introduces a few tips on how to easily introduce bugs in JavaScript programming. This article summarizes eight tips. if you cannot understand these tips, it will cause you a BUG in programming, if you need it, you can refer to JavaScript, which is one of the most popular programming languages today, but it is also a side effect of the features of the language. 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 ):

The 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

The 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

The 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

The 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?

The code is as follows:


Typeof null // object
Null instanceof Object // false

5. return returned content

The 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

The 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

The 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

The 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.