Home >Web Front-end >JS Tutorial >Some tips on how to easily cause bugs in JavaScript programming

Some tips on how to easily cause bugs in JavaScript programming

PHPz
PHPzOriginal
2016-05-16 16:16:451674browse

This article mainly introduces some tips that are prone to bugs in JavaScript programming. This article summarizes 8 tips. If you don’t understand these tips, they will cause you trouble and bugs in programming. You need Friends can refer to

JavaScript is one of the most popular programming languages ​​today, but its popularity is also a side effect of the various features of the language itself. No matter how beautiful the language is, there are still thousands of people using it every day Tens of thousands of programmers created a bunch of bugs. Don't laugh at others yet, maybe you are one of them.

To give you an example, here are a few short JS snippets that are completely valid (you can try it out on your console):

typeof NaN === 'number' // true
 
Infinity === 1/0        // true
0.1 + 0.2 === 0.3       // false,前面加括号也一样
"3" + 1                 // '31'  
"3" - 1                 // 2

Do you still believe it? Your own JavaScript?

1. The smallest value in JS

Number.MIN_VALUE > 0; //true

Number.MIN_VALUE is used for the smallest value that JavaScript can express, which is 5e-324, but it is the closest in JS A number of 0

2. String connection

("foo" + + "bar") === "fooNaN"  //true
"why I am " + typeof + ""       // why I am number

JS is parsed into "foo" ("bar"), which will convert "bar" into a number

3. parseInt function

parseInt('06'); // 6
parseInt('08'); // 0  注意,谷歌新版已修正
parseInt(null, 24) === 23 // true

4. Is null an object

typeof null  // object
null instanceof Object  // false

5. The content returned by return

function myjson()
{
   return
   [
     2
   ]
}
myjson();  // undefined

The content returned by return must be on the same line as return

6. Strange numbers

 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

function hello(what) {
     alert(arguments[0]);    //vicky
     what = "world";
     return "Hello, " + arguments[0] + "!";
}
hello("vicky"); //"Hello, world!"

8. The head-scratching equal sign

NaN === NaN;   // false
[] == false;   // true
"" == false;   // true
null == false; // false
[] == ![]      // true
window.window == window  // true
window.window === window // false,有些浏览器是true
window == document       // true,有些浏览器是false
("0" && {}) == 0 // false
(0 && {}) == 0   // true
0 == "0"         // true
[] == 0          // true

That’s it For the entire content of this chapter, please visit JavaScript Video Tutorial for more related tutorials!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn