How does an undefined variable throw a type error?


1252748

I have a user that is getting the error

TypeError: a is undefined

I'm confused how this happened. Won't trying to access an undefined variable raise a reference error? Under what circumstances would a type error be thrown?

Disi

As @jgillich pointed out in his answer, the following code produces one TypeErroron the undefinedobject .

> a
ReferenceError: a is not defined
> var a;
> a.x
TypeError: a is undefined

To see why, see Section 11.2.1, "Property Accessors" of the ECMAScript 5.1 specification . We are interested in step 5

5. Call CheckObjectCoercible( baseValue ).

In our example, baseValue is the value of the reference a. This means baseValue is undefined.

CheckObjectCoercibleDefined in Section 9.10

Raises an error if the argument to the abstract operation CheckObjectCoercible is a value that cannot be converted to Object using ToObject. Defined by Table 15:

We can see in Table 15 that one TypeErroris thrown undefinedand the nullvalue.

So , as usual, the reason we use a TypeErrorinstead of a ReferenceErroris because the spec says so!

There are other ways to get TypeErroron undefined, in particular ToObject can also throw TypeErrorfor undefined.

These three lines of code produce TypeError: can't convert undefined to object:

Object.defineProperties({}, undefined);
Object.prototype.toLocaleString.call(undefined);
Object.prototype.valueOf.call(undefined);

Although this time the message is clearer.

Also called directly on the undefinedproductTypeError: undefined has no properties

undefined.foo();
undefined.x;

All tested with Firefox 33.0a2 (Aurora).

Related


How does an undefined variable throw a type error?

1252748 I have a user that is getting the error TypeError: a is undefined I'm confused how this happened. Won't trying to access an undefined variable raise a reference error? Under what circumstances would a type error be thrown? Disi As @jgillich pointed ou

How does an undefined variable throw a type error?

1252748 I have a user that is getting the error TypeError: a is undefined I'm confused how this happened. Won't trying to access an undefined variable raise a reference error? Under what circumstances would a type error be thrown? Disi As @jgillich pointed ou

DB::transaction throw undefined variable error exception

Govind Sanlo Works on Laravel 5 DB::transaction but throws the following error: Exception 'ErrorException' with message 'Undefined variable: task' Here is my code: private function addTasks($tasks, $id) { DB::transaction(function() { DB::table('task')->w

DB::transaction throw undefined variable error exception

Govind Sanlo Works on Laravel 5 DB::transaction but throws the following error: Exception 'ErrorException' with message 'Undefined variable: task' Here is my code: private function addTasks($tasks, $id) { DB::transaction(function() { DB::table('task')->w

DB::transaction throw undefined variable error exception

Govind Sanlo Works on Laravel 5 DB::transaction but throws the following error: Exception 'ErrorException' with message 'Undefined variable: task' Here is my code: private function addTasks($tasks, $id) { DB::transaction(function() { DB::table('task')->w

Why does jsfiddle throw an error of undefined function?

Thomas The example I tested is very simple: there is only one button, and when the button is clicked, a function is called. However, I get: Error ReferenceError: AddNewData is not defined {"error":"Please use a POST request"} Here is the jsfiddle link : http:/

Why does jsfiddle throw an error of undefined function?

Thomas The example I tested is very simple: there is only one button, and when the button is clicked, a function is called. However, I get: Error ReferenceError: AddNewData is not defined {"error":"Please use a POST request"} Here is the jsfiddle link : http:/

Why does jsfiddle throw an error of undefined function?

Thomas The example I tested is very simple: there is only one button, and when the button is clicked, a function is called. However, I get: Error ReferenceError: AddNewData is not defined {"error":"Please use a POST request"} Here is the jsfiddle link : http:/

Why does jsfiddle throw an error of undefined function?

Thomas The example I tested is very simple: there is only one button, and when the button is clicked, a function is called. However, I get: Error ReferenceError: AddNewData is not defined {"error":"Please use a POST request"} Here is the jsfiddle link : http:/

Why does jsfiddle throw an error of undefined function?

Thomas The example I tested is very simple: there is only one button, and when the button is clicked, a function is called. However, I get: Error ReferenceError: AddNewData is not defined {"error":"Please use a POST request"} Here is the jsfiddle link : http:/

Why does undefined?.fun() not throw an error?

Andrew Ray I was surprised to see the following behavior in Node 14.18.0: > {}?.fun(); Uncaught TypeError: b?.fun is not a function > undefined?.fun(); undefined I understand why the first statement throws a TypeError. {}?.funis undefined, which is not calla

.. undeclared variable in if statement does not throw error

Nelson Ovalo I just noticed some weird behavior in Javascript. To write short code, I came up with the following function: if(a = 'A|B|C'.split('|'), a.length > 1){ // I have access to a, but it was never declared as a variable? } else { // some code } //

.. undeclared variable in if statement does not throw error

Nelson Ovalo I just noticed some weird behavior in Javascript. To write short code, I came up with the following function: if(a = 'A|B|C'.split('|'), a.length > 1){ // I have access to a, but it was never declared as a variable? } else { // some code } //

.. undeclared variable in if statement does not throw error

Nelson Ovalo I just noticed some weird behavior in Javascript. To write short code, I came up with the following function: if(a = 'A|B|C'.split('|'), a.length > 1){ // I have access to a, but it was never declared as a variable? } else { // some code } //

Why does this map declaration throw a type error?

John Morrison I try to declare a variable where each key in the map is an array of objects. For some reason the act of making the value an array throws a TypeError. This is allowed: var map = new Map([ ['a', {c: 1}], ['b', {c: 1, d: 1}]

Why does this map declaration throw a type error?

John Morrison I try to declare a variable where each key in the map is an array of objects. For some reason the act of making the value an array throws a TypeError. This is allowed: var map = new Map([ ['a', {c: 1}], ['b', {c: 1, d: 1}]

Testing BehaviorSubject does not throw access type error

User 1019042 I'm trying to test a behaviorSubject but keep getting: Error: <spyOnProperty> : Property myBehaviorSubject$ does not have access type get This is how I declare my behaviorSubject in myService: myBehaviorSubject$ = new BehaviorSubject<Array<Object

Scala type bounds - does it throw an error?

Shookit I'm currently trying to wrap my head around the scala type scope. From what I understand, they are used to limit the types available for generics based on type-based hierarchies. It seems to me that the following code does not work properly; testdef ta

throw undefined variable

Kevin This is a freecodecamp question where the string parameter should be multiplied by the num parameter. I understand the other methods they offer, but I'm confused as to how this method works. repeatStringNumTimes("abc", 3); //should return "abcabcabc" I

Why does NDK throw Undefined Reference error on Build?

BramGoderie I am getting this error when trying to build the library with the ndk: $ /cygdrive/d/android-ndk-r10d/ndk-build.cmd [armeabi] SharedLibrary:libgame.so jni/core/src/coreApplication.cpp:9: error: undefined reference to 'CCoreMessaging::CCoreMessaging

Why does NDK throw Undefined Reference error on Build?

BramGoderie I am getting this error when trying to build the library with the ndk: $ /cygdrive/d/android-ndk-r10d/ndk-build.cmd [armeabi] SharedLibrary:libgame.so jni/core/src/coreApplication.cpp:9: error: undefined reference to 'CCoreMessaging::CCoreMessaging

Why does NDK throw Undefined Reference error on Build?

BramGoderie I am getting this error when trying to build the library with the ndk: $ /cygdrive/d/android-ndk-r10d/ndk-build.cmd [armeabi] SharedLibrary:libgame.so jni/core/src/coreApplication.cpp:9: error: undefined reference to 'CCoreMessaging::CCoreMessaging

Why does NDK throw Undefined Reference error on Build?

BramGoderie I am getting this error when trying to build the library with the ndk: $ /cygdrive/d/android-ndk-r10d/ndk-build.cmd [armeabi] SharedLibrary:libgame.so jni/core/src/coreApplication.cpp:9: error: undefined reference to 'CCoreMessaging::CCoreMessaging

Why does NDK throw Undefined Reference error on Build?

BramGoderie I am getting this error when trying to build the library with the ndk: $ /cygdrive/d/android-ndk-r10d/ndk-build.cmd [armeabi] SharedLibrary:libgame.so jni/core/src/coreApplication.cpp:9: error: undefined reference to 'CCoreMessaging::CCoreMessaging