List of Javascript traps to help avoid errors

Source: Internet
Author: User

I can't talk about the translation of "Xin \ YA \ da". I hope I can make a mistake in the article.

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.

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

  1. <Script>
  2. Alert (0.02/0.1); // 0.19999999999999998
  3. Alert (1.14*100); // 113.99999999999999
  4. </Script>

Math. round () can be used here.

2. overload of the plus sign Operator

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

  1. <Script>
  2. Var msg, one = "1 ";
  3. Msg = 2 + "1"; // msg = "21"
  4. Msg = 2 + one; // msg = "21"
  5. Msg = 1 + 1 + 1 + "musketeers"; // msg = "3 musketeers"
  6. Msg = "Bond" + 0 + 0 + 7; // msg = "Bond 007"
  7. </Script>

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

3. Insert a semicolon at the end of the row

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

  1. <Script>
  2. Function returnSame (){
  3. Return // Inserts semi-colon to convert to return;
  4. A // a becomes a;-Unreachable
  5. }
  6. Alert (returnSame (2); // Output is undefined
  7. </Script>

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

4. typeof Operator

Typeof is a unary operator, and the computation result is often not as expected. What is surprising is that the computation result for "null" is "object"

  1. <Script>
  2. Var obj = {}; // object created using object literal
  3. Var arr = []; // array created by array literal
  4. Alert (typeof (obj); // object-Good
  5. Alert (typeof (arr); // object-Bad
  6. Alert (typeof (null); // object-uugly!
  7. </Script>

It can only find the original type of the object.

5. false, null, undefined, NaN, Infinity

Although they look similar, they do not mean anything. 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.

  1. <Script>
  2. Var;
  3. Alert (a); // undefined
  4. Alert (1/0); // Infinity
  5. Alert (0/0); // NaN
  6. 0/0 = 0/0; // false-a NaN! = NaN
  7. Alert (B); // error
  8. </Script>

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

  1. <Script>
  2. Var nospace = "I dont need spaces". replace ("","_");
  3. Alert (nospace); // I _dont need spaces-Only first occurence
  4. Var nospace = "I dont need spaces". replace (// g ,"_");
  5. Alert (nospace); // I _dont_need_spaces
  6. </Script>

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

  1. <Script>
  2. Var str = "017 ";
  3. Var strInt = parseInt (str); // strInt = 15
  4. Var strInt = parseInt (str, 10); // strInt = 17
  5. </Script>

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.