Some programming traps of JavaScript

Source: Internet
Author: User
Programming traps (gotcha) refer to unexpected document features in computer systems rather than bugs. These

Programming traps (gotcha) refer to unexpected document features in computer systems rather than bugs. These traps keep beginners away from javascript programming. In my opinion, because all browsers can run javascript, it is one of the most widely used languages, but it is also researched by at least people. Let's start with a basic example.

Floating Point Operation

This may be the main reason to drop people who are not familiar with javascript and are ready to execute some mathematical operations.

《script》alert(0.02 / 0.1);  //0.19999999999999998alert(1.14 * 100);  //113.99999999999999;)《script》  

Math. round () can be used here.

Overload of the plus sign Operator

The plus sign operator can perform arithmetic operations and connect strings. It is very convenient to use it correctly. Let's take a look.

《script》var msg, one="1";msg = 2 + "1"; // msg = "21"msg = 2 + one; // msg = "21"msg = 1 + 1 + 1 + "musketeers" // msg = "3 musketeers"msg = "Bond" + 0 + 0 + 7; //msg = "Bond 007"《script》  

These operations are performed from left to right. Type conversion is based on strings or numbers.

Insert a semicolon at the end of a row

Javascript automatically inserts a semicolon ";" at the end of a row. Let's take a look at this situation in a simple example.

《script》function returnSame(a){return //Inserts semi-colon to convert to return;a                      //a becomes a; - Unreachable}alert(returnSame(2));  //Output is undefined《script》  

This magic semicolon can make things more complex when creating an object or using the value of an object.

Typeof Operator

Typeof is a unary operator, and the computation result is often not as expected. Surprisingly, the result of the "null" operation is "object ".

《script》var obj={}; //object created using object literalvar arr=[]; //array created by array literalalert(typeof(obj)); //object  - Goodalert(typeof(arr)); //object  - Badalert(typeof(null)); //object  - Ugly!  ;)《script》  

It can only find the original type of the object.

False, null, undefined, NaN, Infinity

Although they look similar, they mean different things. Javascript has three basic data types: numbers, strings, and boolean. In addition, there are two unimportant data types: undefine and null ". According to the "=" operator, null and undefine are equal.

《script》var a;alert (a);    //undefinedalert (1/0);  //Infinityalert (0/0);  //NaN0/0 == 0/0;   //false - a NaN != NaNalert (b);    //error《script》  
The string must replace the first matched character.

Unlike PHP or other programming languages, by default, the character replacement of javascript only replaces the first matched character.

《script》var nospace = "I dont need spaces".replace(“ ”,“_”);alert(nospace);    //I_dont need spaces   - Only first occurencevar nospace = "I dont need spaces".replace(/ /g,“_”);alert(nospace);    //I_dont_need_spaces《script》  
ParseInt Function

ParseInt is used to convert a string to the integer type. This function can input two parameters. The second parameter specifies the number of hexadecimal parameters. In decimal format, 10 is used. If the hexadecimal value is not specified, the parseInt function will try to find the appropriate hexadecimal value. If so, the string starting with 0 will be converted to octal.

《script》var str = "017";var strInt = parseInt(str); //strInt = 15  ;)var strInt = parseInt(str,10); //strInt = 17《script》  

Address of this article: http://www.nowamagic.net/librarys/veda/detail/751,welcome.

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.