Search code examples
javascriptfirefoxtypeerrorreferenceerror

Difference TypeError and ReferenceError


What's the differnce between

TypeError: ... is undefined

and

ReferenceError: ... is not defined

?


Solution

  • A ReferenceError occurs when you try to use a variable that doesn't exist at all.

    A TypeError occurs when the variable exists, but the operation you're trying to perform is not appropriate for the type of value it contains. In the case where the detailed message says "is not defined", this can occur if you have a variable whose value is the special undefined value, and you try to access a property of it.

    See http://javascriptweblog.wordpress.com/2010/08/16/understanding-undefined-and-preventing-referenceerrors/ for some discussion related to this.