English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Basic Tutorial

JavaScript Objects

JavaScript Functions

JS HTML DOM

JS Browser BOM

AJAX Basic Tutorial

JavaScript Reference Manual

Common JavaScript Errors

JavaScript is one of the most popular programming languages in the industry today. If you want to learn this language, please avoid the following mistakes.

Unexpected use of the assignment operator

If we are not careful and use the assignment operator (=) instead of the comparison operator (==) in the if statement, the JavaScript program may produce unexpected results.

If num does not equal20, then this if statement returns false:

var num = 0;
if (num == 20)
Test and see‹/›

This if statement returns true because20 is true:

var num = 0;
if (num = 20)
Test and see‹/›

Assignment always returns the assigned value.

Using '==' instead of '===

This may be the most common mistake when people start using JavaScript.

In conventional comparison, the data type is not important. If the if statement returns true:

var a = 20;
var b = "20";
if (a == b)
Test and see‹/›

In strict comparison, the data type is indeed very important. This if statement returns false:

var a = 20;
var b = "20";
if (a === b)
Test and see‹/›

The difference between (a == b) and (a === b) is:

  • == If a is equal to b, it returns true

  • === If a is equal to b and they belong toSame typeIf the condition is met, it returns true

usually, you should always use ===, also known as the strict equality operator or identity operator.

confusing addition and concatenation

As can be known from the previous chapters,+operators are used for addition and concatenation.

Additionallyis about addingnumbers.

concatenationis about addingThe string's.

Therefore, adding numbers as numbers and adding numbers as strings will produce different results:

var a = 10;
var b = 5;
var c = a + b; // c = 15
var a = 10;
var b = "5";
var c = a + b; // c = "105"
Test and see‹/›

Incorrect use of float

All numbers in JavaScript are stored as64Bitwise floating-point numbers (Floats).

All programming languages (including JavaScript) have difficulty with exact floating-point values:

var a = 0.1;
var b = 0.2;
var c = a + b;   // The result of c will not be 0.3
Test and see‹/›

To solve the above problem, it helps to multiply and divide:

var c = (a * 10 + b * 10) / 10; // c = 0.3
Test and see‹/›

JavaScript string line breaks

JavaScript will allow you to split a statement into two lines:

var str =
"w3codebox Tutorial";
Test and see‹/›

However, breaking a statement in the middle of a string will not work:

var str = "w3codebox
Tutorial";
Test and see‹/›

If it is necessary to break a statement in the middle of a string, you must use the "backslash" (escape character):

var str = "w3codebox \
Tutorial";
Test and see‹/›

    Another solution is to useTemplate literals(backticks ``)string. These eliminate the need for escaping long strings:

var str = `Air Pollution is the introduction of chemicals to the
atmosphere. It damages environmental balance and causes
several diseases.`;
Test and see‹/›

Incorrect semicolon placement

Due to the incorrect placement of the semicolon, the following code will always be executed regardless of the value of num:

if (num === 45);
{
  // code block  
}
Test and see‹/›

definitions ending with a comma

The trailing comma in object and array definitions in ECMAScript 5is valid in the middle.

let fruits = ["Apple", "Mango", "Banana", "Orange",];//array
let user = {firstName:"Vishal", lastName:"Choudhary", age:22,};//object

However, adding a comma at the end of an array, Internet Explorer 8will crash.

JSON does not allow trailing commas.

Block Scope

JavaScript does not create a new scope for each code block.

is applicable in many programming languages, but not in JavaScript.

for (var i = 0; i < 10; i++) {
  // some code
}
document.write(i);   // What will this output be?
Test and see‹/›

usedletkeywords create a new scope for each code block:

for (let i = 0; i < 10; i++) {
  // some code
}
document.write(i);   // What will this output be?
Test and see‹/›

Difference between null and undefined

undefinedvalue indicates that a value has not been assigned to the variable, or the variable has not been declared at all.

nullvalue indicates that there is no object value intentionally.

this makes it a bit difficult to test whether an object is empty.

it is possible to test whether the type isundefinedto test whether an object exists:

if (typeof myObj === "undefined")
Test and see‹/›

    but you cannot test whether an object isnullbecause if the objectundefinedwhich will throw an error:

if (myObj === null)
Test and see‹/›

To solve this problem, it is necessary to test whether an object is notundefinedand notnull:

if (typeof myObj !== "undefined" && myObj !== null)
Test and see‹/›