For detect undefined variable which have been declared in javascript there at least two different ways.
var bar; // declared variable
if (typeof bar !== "undefined") {
// make something with bar
}
if (bar) {
// make something with bar
}
My Questions:
1) Are the two ways equal in all the aspects?
2) If not what is the best?
if (bar) {will return false for many cases other than an undefined variable.For instance, if bar is 0 then
if(bar)will evaluate to false. bar having a value offalsewill also evaluate to false.