1. syntaxError 語法錯誤
- 變數名不規範:
var 1 = 1; // Uncaught SyntaxError: Unexpected number
var 1a = 1; // Uncaught SyntaxError: Invalid or unexpected number
- 關鍵字賦值:
new = 1; // Uncaught SyntaxError: Unexpected token =
- 基本語法錯誤:
var a = 5: // Uncaught SyntaxError: Unexpected token :
2. ReferenceError 引用錯誤
- 變數或者函式未被宣告:
fn(); // Uncaught ReferenceError: fn is not defined
- 給無法被賦值的物件賦值時:
console.log() = 2; // Uncaught ReferenceError: Invalid left-hand side in assignment
3. RangeError 範圍錯誤
- 陣列長度賦值為負數:
var arr = [1, 2, 3];
arr.length = -1; // Uncaught RangeError: Invalid array length
- 物件方法引數超出可行範圍
var num = 66.66;
num.toFixed(-1); // Uncaught RangeError: toFixed() digits argument must be between 0 and 100
4. TypeError 型別錯誤
- 呼叫不存在的方法:
123(); // Uncaught TypeError: 123 is not a function
// 會先判斷是否可能是函式,像123就不可能是函式,所以直接報型別錯誤,而如果是合法的函式名,則報ReferenceError
var obj = {}
obj.say(); // Uncaught TypeError: obj.say is not a function
- 例項化原始值:
var a = new 1; // Uncaught TypeError: 1 is not a constructor
5. URIError URI錯誤
URI: URIFORM RESOURCE IDENTIFIER 統一資源識別符號
URL: URIFORM RESOURCE LOCATOR 統一資源定位符
URN: URIFORM RESOURCE NAME 統一資源識別符號
URI包括URL和URN
decodeURI("%abc"); // Uncaught URIError: URI malformed