Why does accessing a non-existent object property result in `undefined` instead of throwing a `ReferenceError`?

Because object properties are not variables. The rules are different. Accessing an object property that doesn’t exist gives you undefined, not an error. It’s just the way the language is designed.

One possible explanation for the difference, other than just that “that’s how Eich designed it,” is that you don’t declare object properties. You just use them. But variables must be declared (other than The Horror of Implicit Globals, and we don’t have that now we have strict mode).

Leave a Comment