1

Why did javascript not throw a type error when I try to access a property that does not exist?

My code is below

var array = [];
var someBoolean1 = array[0] && array[0].hey;

The value of someBoolean turned out be 'undefinded'; I expect javaScript will throw a type error;

When I do

var array = [];
var someBoolean2 = array[0].hey;

The typeerror is thrown as expected.

But I do not understand why there is no type error in the first block of code. Any idea?

CherylG
  • 1,032
  • 8
  • 23

5 Answers5

9

Because you explicitly guard against the type error...

array[0] && array[0].hey

array[0] is undefined, so:

undefined && array[0].hey

undefined is falsey, so the right hand side is not evaluated, so the result of this expression is the left hand value undefined, which is assigned to someBoolean1.


Tangentially... to make such expressions less repetitive, usually this idiom is used:

var someBoolean1 = (array[0] || {}).hey;

If array[0] is undefined, {} will be used instead, and then the property .hey of that object will be accessed. If array[0] is an object with a property .hey, you'll get its value, otherwise you'll safely get undefined out of the entire expression.

deceze
  • 510,633
  • 85
  • 743
  • 889
2

No type error is thrown because your and operator is short circuiting, array[0] is undefined, which is not true, meaning javascript doesn't feel the need to check the second part of your and condition, and doesn't realize that there is a type error.

Alden Be
  • 508
  • 3
  • 15
0
 if (false && "any another statement is unreachable")  
   then do something;

second expression not evaluated as array[0] false array[0].hey is not reached

var someBoolean1 = array[0] && array[0].hey;
asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84
0

It happens because of the execution of && expression. array[0] happend to be evaluated to false so the rest of the expression is not evaluated.

Ivan Gritsenko
  • 4,166
  • 2
  • 20
  • 34
0

Short Answer:

Because there is no element at the index 0 of array, and in more generic words, the property 0 is not defined in variable array. Therefore calling a property on something that is not defined would give you an error.

Long Answer:

Consider all JavaScript objects as dictionaries, and lets think that these dictionaries return us the values if we provide the keys that exist in them, otherwise nothing is returned.

Therefore when accessing an object property like myObject.propA (which is same as myObject["propA"]), if it exists in this object then the value of that property is returned and if not then undefined is returned, which denotes that this property is not defined in the myObject.

An array ([]) in JavaScript is also a type of object, and each member in the array is actually the property of that object. So a property in var arr = ["asd", "fgh"] can be accessed by arr[0] or arr["0"], which means that if property 0 of arr exists then it would return the value (in this case "asd"), otherwise would return undefined.

Providing all this, calling a property on something that is undefined is both illogical and illegal, so an exception is raised.

Ammar Hasan
  • 2,436
  • 16
  • 22