Differences between TypeError and ReferenceError

What is the difference

TypeError: ... is undefined

and

ReferenceError: ... is not defined

?

+6
source share
3 answers

A ReferenceErroroccurs when you try to use a variable that does not exist at all.

A TypeErroroccurs when a variable exists, but the operation you are trying to perform is not suitable for the type of value it contains. In the case where the detailed message says "not defined", this can happen if you have a variable whose value is a special value undefinedand you are trying to access its property.

. http://javascriptweblog.wordpress.com/2010/08/16/understanding-undefined-and-preventing-referenceerrors/ , .

+13

JavaScript:

JavaScript 1.5 :

EvalError: , eval() .

RangeError: , .

ReferenceError: , .

SyntaxError: , JavaScript.

TypeError: , .

strong text URIError: , encodeURI() decodeURI() .

+12

:

function foo(){
 var d=1234;
 console.log(d.substring(1,2));     
}
foo();

:

: TypeError: d.substring , () (, ). TypeError , .

function foo(){
 var d=1234;
 console.log(c);
}
foo();

:

: ReferenceError: c , "c" , , . ReferenceError , .

+3

All Articles