JavaScript Usage Myths

Source: Internet
Author: User

1.JavaScript Usage Error

1.1 Assignment operator Application error

In a JavaScript program, if you use the equal sign (=) of the assignment operator in an IF condition statement, the correct method is to use the two equals sign (= =) of the comparison operator.

The if condition statement returns false (as we expected) because X is not equal to 10:

var 0 ; if Ten)

The if condition statement returns true (not what we expected) because the conditional statement executes for x assignment 10,10 to true:

var 0 ; if Ten)

The if condition statement returns false (not what we expected) because the conditional statement executes for x assignment 0,0 to false:

var 0 ; if 0)

1.2 Common errors for comparison operators

In a general comparison, the data type is ignored, and the following if condition statement returns true:

var Ten ; var " Ten " ; if (x = = y)

In a strict comparison operation, the = = = is an identical calculation character, while checking the value and type of the expression, the following if condition statement returns false:

var Ten ; var " Ten " ; if (x = = y)

This error often occurs in a switch statement, and the switch statement is compared using the identity calculation (= = =):

The following example executes the alert pop-up window:

var Ten ; Switch (x) {    caseten: Alert ("Hello");}

The following instances do not execute the Alert pop-up window because of type inconsistency:

var Ten ; Switch (x) {    case"ten": Alert ("Hello" );}

1.3 Addition and connection considerations

An addition is two numbers added.

The connection is a two- string connection.

Both the addition and connection of JavaScript use the + operator.

Next we can see the difference between the two numbers plus the number and the string connection through an example:

var Ten 5;          // The result of X is var Ten " 5 ";        // x results in "zero"

The result of using a variable addition is also inconsistent:

var Ten ; var 5 ; var z = x + y;           // The result of Z is var Ten ; var " 5 " ; var z = x + y;           // The result of Z is " the"

1.4 Usage considerations for floating-point data

All data in JavaScript is stored as 64-bit floating-point data (float) .

All programming languages, including JavaScript, are difficult to determine the accuracy of floating-point data:

var 0.1 ; var 0.2 ; var z = x + y            //  z result is 0.3if0.3)            //  return false< /c14>

In order to solve the above problems, we can use the integer multiplication method to solve the problem:

var Ten Ten ten;       // The result of Z is 0.3

1.5JavaScript String Branch

JavaScript allows us to use a break statement in a string:

var x ="Hello world! ";

However, using a carriage return directly in a string will cause an error:

var " Hello world! " ;

A string break requires a backslash (\), as shown below:

var " Hello \ world! " ;

1.6 Incorrect use of semicolons

In the following instance, if statement loses the method body, the method body of the original if statement is executed as a separate block of code, resulting in an incorrect output result.

Because the semicolon uses an error, the code block in the IF statement is bound to execute:

if  + ); {    //  code block  }

1.7Return Statement Usage Considerations

JavaScript defaults to automatically end in the last line of code.

The following two instances return the same result (one with a semicolon is not):

function MyFunction (a) {    var)      return A * power}
function MyFunction (a) {    var;     return A * power;}

JavaScript can also use multiple lines to end a statement.

The following instance returns the same result:

function MyFunction (a) {    var    ;       return A * power;}

However, the following instance results return undefined:

function MyFunction (a) {    var    ;       return * power;}     

Why is there such a result? Because in JavaScript, the code for instance 4 is consistent with the following code:

function MyFunction (a) {    var    ;       return;       // end of semicolon, return undefined    A * power;}

In JavaScript, semicolons are optional.

Because return is a complete statement, JavaScript closes the return statement.

Note: you do not have to break the return statement.

1.8 using names to index in arrays

Many programming languages allow names to be used as an index to an array.

An array that uses a name as an index is called an associative array (or hash).

JavaScript does not support using names to index arrays, only numeric indexes are allowed.

 var  person = [];p erson[ 0 ] = "  john   " ;p erson[ 1 ] =  " doe   " ;p erson[ 2 ] = 46  ;          var  x = person.length; //  Person.length returns 3              var  y = Person[0 ]; //  Person[0] returns "John"  

In JavaScript, objects use names as indexes .

If you use a name as an index, JavaScript will redefine the array as a standard object when accessing the array.

After doing so, the methods and properties of the array will no longer be used, or an error will be generated:

varperson =[];p erson["FirstName"] ="John";p erson["LastName"] ="Doe";p erson[" Age"] = $;varx = person.length;//person.length return 0vary = person[0];//Person[0] return undefined

1.9 Define array elements and cannot add commas at the end

Adding a comma Although the syntax is not a problem, but in different browsers may get different results.

var colors = [567// so the length of the array may be 3 or 4. 

1.10 Define object, cannot add comma at last

How the error is defined:

Websites = {site:" Rookie tutorial ", url:"www.runoob.com", like: 460,}

1.11Undefined is not Null

In JavaScript, null is used for objects, undefined for variables, properties, and methods.

The object can be null only if it is defined, otherwise undefined.

If we want to test whether an object exists, an error will be thrown when the object is not defined.

How to use the error:

if NULL typeof " undefined "

The correct way is that we need to use TypeOf first to detect whether an object is defined:

if (typeof"undefined"null

1.12 Program block Scope

JavaScript does not create a new scope in each code block, and the scope of each code block is generally global.

The variable I of the following code returns 10, not undefined:

 for (var0; i++    ) {//  some code}return i;

JavaScript Usage Myths

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.