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

May 16, 2016 pm 04:16 PM
bug javascript programming

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
Copy after login

Do you still believe it? Your own JavaScript?

1. The smallest value in JS

Number.MIN_VALUE > 0; //true
Copy after login

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
Copy after login

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
Copy after login

4. Is null an object

typeof null  // object
null instanceof Object  // false
Copy after login

5. The content returned by return

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

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
Copy after login

7. Weird parameters

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

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
Copy after login

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

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Remove duplicate values ​​from PHP array using regular expressions Remove duplicate values ​​from PHP array using regular expressions Apr 26, 2024 pm 04:33 PM

How to remove duplicate values ​​from PHP array using regular expressions: Use regular expression /(.*)(.+)/i to match and replace duplicates. Iterate through the array elements and check for matches using preg_match. If it matches, skip the value; otherwise, add it to a new array with no duplicate values.

Apple iOS18 bug summary Apple iOS18 bug summary Jun 14, 2024 pm 01:48 PM

As Apple's WWDC conference 2024 came to a successful conclusion, not only macos15 was announced, but the update of Apple's new iOS18 system attracted the most attention. Although there are many new features, as the first version of Apple's iOS18, people inevitably wonder whether it is necessary to upgrade Apple. iOS18, what kind of bugs are there in the latest release of Apple iOS18? After real use evaluation, the following is a summary of Apple iOS18 bugs, let’s take a look. Currently, many iPhone users are rushing to upgrade to iOS18. However, various system bugs are making people uncomfortable. Some bloggers said that you should be cautious when upgrading to iOS18 because "there are so many bugs." The blogger said that if your iPhone is

What is programming for and what is the use of learning it? What is programming for and what is the use of learning it? Apr 28, 2024 pm 01:34 PM

1. Programming can be used to develop various software and applications, including websites, mobile applications, games, and data analysis tools. Its application fields are very wide, covering almost all industries, including scientific research, health care, finance, education, entertainment, etc. 2. Learning programming can help us improve our problem-solving skills and logical thinking skills. During programming, we need to analyze and understand problems, find solutions, and translate them into code. This way of thinking can cultivate our analytical and abstract abilities and improve our ability to solve practical problems.

Build browser-based applications with Golang Build browser-based applications with Golang Apr 08, 2024 am 09:24 AM

Build browser-based applications with Golang Golang combines with JavaScript to build dynamic front-end experiences. Install Golang: Visit https://golang.org/doc/install. Set up a Golang project: Create a file called main.go. Using GorillaWebToolkit: Add GorillaWebToolkit code to handle HTTP requests. Create HTML template: Create index.html in the templates subdirectory, which is the main template.

Collection of C++ programming puzzles: stimulate thinking and improve programming skills Collection of C++ programming puzzles: stimulate thinking and improve programming skills Jun 01, 2024 pm 10:26 PM

C++ programming puzzles cover algorithm and data structure concepts such as Fibonacci sequence, factorial, Hamming distance, maximum and minimum values ​​of arrays, etc. By solving these puzzles, you can consolidate C++ knowledge and improve algorithm understanding and programming skills.

The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

Demystifying C: A Clear and Simple Path for New Programmers Demystifying C: A Clear and Simple Path for New Programmers Oct 11, 2024 pm 10:47 PM

C is an ideal choice for beginners to learn system programming. It contains the following components: header files, functions and main functions. A simple C program that can print "HelloWorld" needs a header file containing the standard input/output function declaration and uses the printf function in the main function to print. C programs can be compiled and run by using the GCC compiler. After you master the basics, you can move on to topics such as data types, functions, arrays, and file handling to become a proficient C programmer.

See all articles