Common error types in JavaScript:

SyntaxError indicates a SyntaxError. TypeError TypeError; RangeError RangeError; ReferenceError ReferenceError;

Common mistakes:

Error type: TypeError

1, Uncaught TypeError: Cannot read property ‘id’ of undefined

Cannot read property “”, cannot set property” “

For example, the above error is the undefined variable new_date. This error occurs when an undefined or null attribute is accessed or set.

Error code:

new_date.id = 1;
Copy the code

Solutions:

var new_date = new Date();  
new_date.id = 1;
Copy the code

2, Uncaught TypeError: Cannot set property ‘id’ of undefined

Cannot read property “”, cannot set property” “

For example, if the variable new_date is undefined or null, this error occurs when an attribute with undefined or null is accessed or set.

Error code:

var new_date;
new_date.id = 1;
Copy the code

Solutions:

var new_date = new Date();  
new_date.id = 1;
Copy the code

3, Uncaught ReferenceError: xx is not defined

Indicates that xx is not defined

Raise a ReferenceError when you refer to an undefined variable; When you use a variable, an error is reported. You need to declare the variable first.

For example: the above error is undefined variable new_date

Solutions:

var new_date = new Date(); 
Copy the code

4, Uncaught SyntaxError: missing xx after argument list

You left out the “xx” symbol at the end of a line of code.

For example: the above error is missing.

Error code:

console.log('new_date==',+new_date; 
Copy the code

Solutions:

console.log('new_date==',+new_date); 
Copy the code

5, Uncaught SyntaxError: Identifier ‘xx’ has already been declared

Indicates that a variable has been declared

When a variable already exists, there is no need to declare it twice.

For example, “new_date” has already been declared

Error code:

var new_date = new Date(); 
let new_date;
Copy the code

Solutions:

var new_date = new Date(); // let new_date; To get rid ofCopy the code

6.Uncaught RangeError: toExponential() argument must be between 0 and 100

Indicates: The value exceeds the function scope

For example, the value is not in the toExponential() range

Error code:

Var num = 2.5; console.log(num.toExponential(-1));Copy the code

Solutions:

Var num = 2.5; console.log(num.toExponential(1));Copy the code

7.Uncaught RangeError: Maximum call stack size exceeded

Indicates: exceeds the maximum stack size

This error occurs when you call a recursive function that does not terminate. You can test it in the Chrome developer console.

For example, this error occurs when calling a recursive function that does not terminate. When using recursion, set a condition to terminate the recursion, otherwise it will loop indefinitely until it runs out of call stack space.

Error code:

var arr = new Array();
function a(arr) {
   a(arr);
}
a(arr);
Copy the code

Solutions:

var arr = new Array();
function a(arr) {
   if (arr.count>0){
      return a(arr);;
   } else {
      return;
   }

}
a(arr);
Copy the code

8.Uncaught TypeError: People is not a constructor

Representation: Use objects or variables that are not constructors as constructors

For example, People is not a constructor.

Error code:

let People = 'a';
new People();
Copy the code

Solutions:

function People(name, age) {
   this.name = name;
   this.age = age;
}
var a = new People('a','18');
Copy the code

If there are mistakes, welcome to correct ~ ~ ~