3

How can we differentiate between var foo; and var foo=undefined;?

typeof foo will return for both "undefined" and foo in window will return for both true;

rlandster
  • 7,294
  • 14
  • 58
  • 96
user1365010
  • 3,185
  • 8
  • 24
  • 43
  • You don't--but why would you need to? – Dave Newton Jun 12 '12 at 15:08
  • Unsure, but some of these posts might be of some help.. http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property-in-javascript and http://stackoverflow.com/questions/2647867/how-to-determine-if-variable-is-undefined-or-null – verisimilitude Jun 12 '12 at 15:09
  • `var foo;` is that `foo = undefined`. Therefore typeof foo returns "undefined"! I would ask: why do you need to do that? (or, what do you want to do?) – Edditoria Jun 12 '12 at 15:13
  • Worth mentioning here is [section `10.5` in the specification](http://es5.github.com/#x10.5), where we have *" 8. For each VariableDeclaration and VariableDeclarationNoIn d in code, in source text order do ... Call env’s SetMutableBinding concrete method passing dn, undefined, and strict as the arguments."* So each variable is automatically initialized with `undefined`. – Felix Kling Jun 12 '12 at 15:16
  • we can regex all the innerHTML of the script tag and see how foo was declared – user1365010 Jun 12 '12 at 15:21

4 Answers4

10

var foo implies var foo=undefined, unless of course undefined is set to something other than undefined.

This can be seen in the Browser Console, if you initialise a variable but don't give it a value, it will have the value undefined (and the type "undefined")

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • even if i change the value of `undefined`, all is the same. http://jsfiddle.net/UCjUK/ – user1365010 Jun 12 '12 at 15:14
  • @user1365010: [Since ES5 you cannot change the value of `windwo.undefined` anymore](http://es5.github.com/#x15.1.1.3). Of course you can create a local variable with name `undefined`: http://jsfiddle.net/gUamp/ – Felix Kling Jun 12 '12 at 15:17
  • 1
    @user1365010: You have to try harder than that (nowadays) to redefine `undefined` (which is a ***very, very, very bad idea***): http://jsfiddle.net/UCjUK/1/ – T.J. Crowder Jun 12 '12 at 15:20
  • It's not that bad, I must only put all my script in `(function(undefined){` and `})(3);` – user1365010 Jun 12 '12 at 15:24
  • Variable names should be a clear indication of what they are used for. There is no valid reason to name a variable "undefined". – Niet the Dark Absol Jun 12 '12 at 15:25
  • @user1365010: It may not be very "hard", but it's certainly bad. Why would you want it? – pimvdb Jun 12 '12 at 15:25
2

How can we difference between var foo; and var foo=undefined;?

There is no difference between them, if this line precedes all uses of foo. (It doesn't have to.) The JavaScript engine sees the line var foo=undefined; like this:

// At top of scope
var foo; // `foo` will have the value `undefined`

// Where the var foo=undefined; line was
foo = undefined;

Note that they are completely different things that happen at different times. The declaration and the "initialization" (it's really an assignment) happen separately. The declaration happens upon entry to the execution unit; the initialization/assignment happens later, as part of the step-by-step code execution. (More: Poor misunderstood var)

This means that if you have this:

foo = 5;
alert(typeof foo); // "number"
var foo = undefined;
alert(typeof foo); // "undefined"

...it is not an error, because the way the engine handles that is:

var foo;
foo = 5;
alert(typeof foo); // "number"
foo = undefined;
alert(typeof foo); // "undefined"
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

A correct example works wonders:

function winFoo() {
  console.log('foo' in window);
}

function startFoo() {
  window.foo = 'bar';
}

function ovrFoo() {
  window.foo = undefined;
}

function removeFoo() {
  delete window.foo;
}

//window.foo === undefined
//window.foo hasn't been defined yet
winFoo(); //false
//so it makes sense that 'foo' is not in window
startFoo(); //window.foo === 'bar'
//window.foo has now been set to 'bar'
winFoo(); //true
//so it makes sense that 'foo' is in window
ovrFoo(); //window.foo === undefined
//window.foo has been overridden with a value of undefined
winFoo(); //true
//but the key 'foo' still exists in window, it just has a value of undefined
removeFoo(); //window.foo === undefined
//the key 'foo' has been removed from window and its value is therefor forgotten
winFoo(); //false
//so it makes sense that 'foo' is not in window

Here's a fiddle

Additionally, declaring var foo outside of any function context instantiates window.foo with a value of undefined if !('foo' in window) (meaning that if you call var foo multiple times in the same code block it doesn't override the existing value if there is one). More importantly, var and function statements are actually hoisted to the top of their scope, which means that:

console.log('foo' in window);
var foo;
console.log('foo' in window);

will print true twice instead of false followed by true.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
0

Way 1:

if (null === var)


(to old browsers):

if (void 0 != var)


another one:

if(var) 


and use it like:

var un;
if (un==undefined)
{
alert('Yes');
} else {
alert('No');
}
roger janety
  • 61
  • 2
  • 8