Help avoid the wrong JavaScript trap list _javascript Tips

Source: Internet
Author: User

Pay attention to "letter", I am far from it. I hope I can put the meaning of the article on the line.

Programming traps (GOTCHA) refer to unexpected document features in computer systems rather than bugs. These traps allow beginners to program from JavaScript. In my opinion, because all browsers can run JavaScript makes it one of the most widely used languages, But it is also the least studied. Let's start with a basic example.

1. Floating-point operations

This may be the main reason for frustrating some people who are unfamiliar with JavaScript and ready to perform 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 useful here.

2. Overloading of the plus operator

The "+" plus operator is capable of arithmetic operations and a string connection. It is convenient to use it correctly. Let's have a look.

  1. <script>
  2. var msg, one="1";
  3. msg = 2 + "1"; //msg = " "
  4. msg = 2 + one; //msg = " "
  5. msg = 1 + 1 + 1 + "musketeers"; //msg = "3 musketeers"
  6. msg = "Bond" + 0 + 0 + 7; //msg = "Bond 007"
  7. </script>

The above behavior is because these operations are performed from left to right. The conversion of a type is based on a string or number within it.

3. Insert semicolon at end of line

JavaScript automatically inserts a semicolon ";" at the end of the line, let's take a look at this in a simple example.

  1. <script>
  2. function Returnsame (a) {
  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 magical semicolon can make things more complicated when you create objects or use the values of objects.

4.typeof operator

typeof is a unary operator, and the results are often not as expected. Surprisingly, the result of the "null" operation was "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-ugly! ;)
  7. </script>

It can only find the original type of the object.

5. False, null, undefined, NaN, Infinity

Although they look similar, they mean nothing. JavaScript has 3 basic data type numeric numbers, String strings and Boolean Boolean, in addition to two unimportant data types "undefine" and "null". Follow "= = "operator operation, NULL and undefine are equal."

  1. <script>  
  2. var  a;  
  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 replaces only the first matching character

Unlike PHP or other programming languages, the character substitution of JavaScript replaces only the first occurrence of a matching character by default.

  1. <script>
  2. var nospace = "I dont need spaces". Replace ("","_");
  3. alert (nospace); //i_dont need spaces-only-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 an integer type. This function can pass in two arguments, and the second parameter is the number of bytes specified. Here the decimal is specified with 10. If you do not specify a system, the parseint function will attempt to find the appropriate system. If so, A string that starts with 0 will be converted to 8.

  1. <script>  
  2. var  str = ;  
  3. var  strint  = parseint (str);       //strint = 15   ;)   
  4. var  strint = parseint (str,10);    //strint =    
  5. </script> 
Related Article

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.