2

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?

antonjs
  • 14,060
  • 14
  • 65
  • 91
  • possible duplicate of [typeof foo\['bar'\] !== 'undefined' vs. 'bar' in foo](http://stackoverflow.com/q/5434368/489560) – Devin Burke Feb 08 '12 at 21:21
  • I suggest a check based on the expected type of the variable. For instance, if you expect a string, you can do `if ( bar.length > 0 ) {...}` to only execute code if you have a non-empty string. – Šime Vidas Feb 08 '12 at 21:24

5 Answers5

2

No they are not equal. if(bar) will also fail if the variable is false or falsy. Use the first method with typeof

LoveAndCoding
  • 7,857
  • 2
  • 31
  • 55
2

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 of false will also evaluate to false.

James Montagne
  • 77,516
  • 14
  • 110
  • 130
1

I suppose this is programmer preference but I don't like variables that are undefined. If they are undefined I will assign them some value.

If you have a situation where a variable is either a reference to an object, or it doesn't reference yet:

var my_ref; // undefined

function ref(that_obj) {
    my_ref = that_obj;
}

console.log(my_ref);    // undefined
ref({"some": "object"});
console.log(my_ref);    // {"some": "object"}

My preference is to initialize it to false. This only works ofcourse if false is never an expected value. Other values to use are -1 for numbers or null.

Another example:

function my_func(arg1, arg2, arg3) {
    if (has_no_value(arg3)) {
        arg3 = "default value";
    }
    console.log(arg1, arg2, arg3);
}
my_func("first", "lol");    // first lol default value

Also, I recommend making a function that checks for undefined-ness (has_no_value in my case) because it's easy to make a mistake in typeof arg3 === "undefined"

if(arg3) is false if arg3 is falsy which includes undefined null 0 "" false NaN

if you want to check for false do if (false === arg3)

Type coercion is used in JavaScript with the == and != operators. From Wikipedia: “There are two types of type conversion: implicit and explicit. The term for implicit type conversion is coercion. The most common form of explicit type conversion is known as casting.” What this means is that true and false are converted to numbers before they are compared to a number. When comparing to any of these values: 0, null, undefined, '', true and false, Douglas Crockford, in JavaScript: The Good Parts, recommends the use of the === or !== operators. In JSLint, there is a “Disallow == and !=” option, which requires the use of === and !== in all cases.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
0

The second one is testing for falsy values which don't necessarily indicate its undefined.

kinakuta
  • 9,029
  • 1
  • 39
  • 48
0

Consider this:

var bar;
bar = 0; 
alert (typeof bar !== "undefined")  // alerts true
alert(bar == true) // alerts false
bfavaretto
  • 71,580
  • 16
  • 111
  • 150