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.
- <script>
- alert (0.02/0.1); //0.19999999999999998
- Alert (1.14 * 100); //113.99999999999999;)
- </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.
- <script>
- var msg, one="1";
- msg = 2 + "1"; //msg = " "
- msg = 2 + one; //msg = " "
- msg = 1 + 1 + 1 + "musketeers"; //msg = "3 musketeers"
- msg = "Bond" + 0 + 0 + 7; //msg = "Bond 007"
- </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.
- <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 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."
- <script>
- var obj={}; //object created using object literal
- var arr=[]; //array created by array literal
- Alert (typeof(obj)); //object-good
- Alert (typeof(arr)); //object-bad
- Alert (typeof(null)); //object-ugly! ;)
- </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."
- <script>
- var a;
- alert (a); //undefined
- alert (1/0); //infinity
- alert (0/0); //nan
- 0/0 == 0/0; //false - a Nan != nan
- alert (b); //error
- </ 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.
- <script>
- var nospace = "I dont need spaces". Replace ("","_");
- alert (nospace); //i_dont need spaces-only-occurence
- var nospace = "I dont need spaces". Replace (//g,"_");
- alert (nospace); //i_dont_need_spaces
- </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.
- <script>
var str = ;
- var strint = parseint (str); //strint = 15 ;)
- var strint = parseint (str,10); //strint =
- </script>