Accessing property of object vs variable in javascript

Posted by Samuel on Stack Overflow See other posts from Stack Overflow or by Samuel
Published on 2012-09-18T21:31:45Z Indexed on 2012/09/18 21:37 UTC
Read the original article Hit count: 206

Filed under:

Why when I try to access a variable that don't exist, javascript throw an exception but when I try to access a property that don't exist in an object, javascript returns an undefined object?

For example, this case returns an undefined object:

function Foo(){
    console.log(this.bar);
}

Foo();

But, in this other example, javascript throw an exception:

function Foo(){
    console.log(bar);
}

Foo();

ReferenceError: bar is not defined

© Stack Overflow or respective owner

Related posts about JavaScript