Latest web development tutorials
×

JavaScript Tutorial

JavaScript Tutorial JavaScript Introduction JavaScript usage JavaScript Output JavaScript grammar JavaScript Statement JavaScript Notes JavaScript variable JavaScript type of data JavaScript Object JavaScript function JavaScript Scope JavaScript event JavaScript The string JavaScript Operator JavaScript Comparison JavaScript If...Else JavaScript switch JavaScript for JavaScript while JavaScript Break & Continue JavaScript typeof JavaScript Type conversion JavaScript Regular Expressions JavaScript error JavaScript debugging JavaScript Variable promotion JavaScript Strict mode JavaScript Use errors JavaScript Form validation JavaScript Keep the keyword JavaScript JSON JavaScript void JavaScript Code specification

JS function

JavaScript Function definition JavaScript Function parameter JavaScript Function call JavaScript Closure

JS HTML DOM

DOM Introduction DOM HTML DOM CSS DOM event DOM EventListener DOM element

JS Advanced Tutorial

JavaScript Object JavaScript Number JavaScript String JavaScript Date JavaScript Array JavaScript Boolean JavaScript Math JavaScript RegExp Object

JS Browser BOM

JavaScript Window JavaScript Window Screen JavaScript Window Location JavaScript Window History JavaScript Navigator JavaScript Pop-ups JavaScript Timing events JavaScript Cookies

JS Library

JavaScript Library JavaScript test jQuery JavaScript test Prototype

JS Examples

JavaScript Examples JavaScript Object instance JavaScript Browser object instance JavaScript HTML DOM Examples JavaScript to sum up

JS Reference Manual

JavaScript Object HTML DOM Object

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)

try it"

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)

try it"

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)

try it"
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)

try it"

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)

try it"

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" );
}

try it"

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

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

try it"

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"

try it"

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"

try it"

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

try it"

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

try it"

JavaScript string Branches

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

Example 1

var x =
"Hello World!";

try it"

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

Example 2

var x = "Hello
World "!;

try it"

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 "!;

try it"

Wrong semicolon

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

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

try it"

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
}

try it"

Example 2

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

try it"

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;
}

try it"

However, the following examples of results are returnedundefined:

Example 4

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

try it"

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"

try it"

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

try it"

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.w3big.com", like:460,}

The correct definition of the way:

websites = {site:"本教程", url:"www.w3big.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;

try it"