"typeof" versus "in" for checking the presence of a property in an object

Viewed 247

I have an object called foo. I want to check if it has the property bar.

Should I use this:

if (typeof foo.bar !== "undefined")

Or this:

if ("bar" in foo)

Is there any difference?

2 Answers
Related