The best Improper Use of JavaScript Tutorial In 2024, In this tutorial you can learn Application assignment operator error,Common Errors comparison,Adding and Connection Considerations,Note the use of floating-point data,JavaScript string Branches,Wrong semicolon,Return statement Precautions,Use an array index names,Define an array element, and finally can not add a comma,Define the object, and finally can not add a comma,Undefined not Null,Block scope,

Improper Use of JavaScript

This chapter we will discuss the use of JavaScript errors.


Application assignment operator error

In JavaScript program if you use a conditional statement if the assignment operator is the equal sign (=) will generate an error result, the correct approach is to use a comparison of two equal signs (==).

if conditional statement returns false (we expect) because x is not equal to 10:

var x = 0;
if (x == 10)

if conditional statement returns true (not that we expected) statement is executed because the conditions for the assignment x 10,10 is true:

var x = 0;
if (x = 10)

if conditional statement returns false (not what we expected) statement is executed because the conditions for the assignment x 0,0 is false:

var x = 0;
if (x = 0)

Note Returns the value of the variable assignment.

Common Errors comparison

In conventional comparison, the data type is ignored, if the following condition statement returns true:

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

In rigorous comparison operation, an identity calculation === operator, while the value of the expression of type checking, the following conditions if statement returns false:

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

This error often occurs in the switch statement, switch statement calculated using the identity operator (===) were compared:

The following examples will execute alert pop:

var x = 10;
switch (x) {
case 10: alert ( "Hello" );
}

The following examples are inconsistent because the type does not perform alert popups:

var x = 10;
switch (x) {
case "10": alert ( " Hello");
}


Adding and Connection Considerations

Addition of two numbers together.

The connection string to connect the two.

Addition and JavaScript are connected using the + operator.

Next, we can see two numbers together and connected to a string of numbers and distinction by way of example:

var x = 10 + 5; // x is 15 Results
var x = 10 + "5" ; Results // x is "105"

Using variables adding the results are inconsistent:

var x = 10;
var y = 5;
var z = x + y; Results // z is 15

var x = 10;
var y = "5";
var z = x + y; Results // z is "105"


Note the use of floating-point data

JavaScript All data are based on 64-bit floating point data type (float) to store.

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

var x = 0.1;
var y = 0.2;
var z = x + y Results // z is 0.3
if (z == 0.3) // Returns false

I solve this problem, you can use an integer multiplication and division to solve:

Examples

var z = (x * 10 + y * 10) / 10; // z result is 0.3


JavaScript string Branches

We use line breaks to run JavaScript statements in the string:

Example 1

var x =
"Hello World!";

However, in the string directly carriage return is being given for:

Example 2

var x = "Hello
World "!;

We can choose the development tool or press F12 to see the error message:

String line breaks need to use a backslash (\), as follows:

Example 3

var x = "Hello \
World "!;


Wrong semicolon

The following example, since the semicolon wrong, if the statement block of code will not be executed:

if (x == 19);
{
// Code block
}


Return statement Precautions

JavaScript is the default automatically ends on the last line of code.

The following two examples return the same result (a not a semicolon):

Example 1

function myFunction (a) {
var power = 10
return a * power
}

Example 2

function myFunction (a) {
var power = 10;
return a * power;
}

JavaScript can also be used to end a multi-line statement.

The following examples return the same result:

Example 3

function myFunction (a) {
var
power = 10;
return a * power;
}

However, the following examples of results are returnedundefined:

Example 4

function myFunction (a) {
var
power = 10;
return
a * power;
}

Why is there such a result? Because in JavaScript, examples of consistent 4 code and the following code:

function myFunction(a) {
    var
    power = 10;  
    return;       // 分号结束,返回 undefined
    a * power;
}

Resolve

If it is an incomplete statement, as follows:

var

JavaScript will attempt to read the second line statement:

power = 10;

However, because this statement is complete:

return

JavaScript will automatically turn off the statement:

return;

In JavaScript, the semicolon is optional.

Since the return is a complete sentence, it will turn off JavaScript return statement.

NoteNote: Do not break the line of the return statement.

Use an array index names

Many programming languages ​​allow the use of the name as the array index.

To use the name as the index of the array is called an associative array (or hash).

JavaScript does not support the use of the name to index arrays, only a numeric index.

Examples

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

In JavaScript, use the name of the object as an index.

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

After performing this operation, methods and properties of the array will no longer be used, otherwise it will generate an error:

Examples

var person = [];
person [ "firstName"] = " John";
person [ "lastName"] = " Doe";
person [ "age"] = 46 ;
var x = person.length; // person.length returns 0
var y = person [0]; // Person [0] returns undefined


Define an array element, and finally can not add a comma

Errors are defined:

points = [40, 100, 1, 5, 25, 10,];

The correct definition of the way:

points = [40, 100, 1, 5, 25, 10];

Define the object, and finally can not add a comma

Errors are defined:

websites = {site:"本教程", url:"www.w3write.com", like:460,}

The correct definition of the way:

websites = {site:"本教程", url:"www.w3write.com", like:460}

Undefined not Null

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

Objects are defined only possible as null, otherwise undefined.

If we want to test whether the presence of the object, when the object is not defined yet will throw an error.

Wrong use:

if (myObj !== null && typeof myObj !== "undefined") 

The right way is that we need to use typeof to detect whether an object has been defined:

if (typeof myObj !== "undefined" && myObj !== null) 

Block scope

JavaScript does not create a new scope in each code block, the general scope of each code block are global.

The following code i variable returns 10 instead of undefined:

Examples

for (var i = 0; i <10; i ++) {
// Some code
}
return i;

Improper Use of JavaScript
10/30