6

Consider the following code:

var x = undefined;

It's a self-contradicting line of code. Is x defined or not? Do implementations of JavaScript remove the variable x from memory, or do they assign it the value undefined?

JSideris
  • 5,101
  • 3
  • 32
  • 50
  • 2
    my brain has just segfaulted – tacone Apr 12 '15 at 23:07
  • possible duplicate of [Setting a variable to undefined](http://stackoverflow.com/questions/2235622/setting-a-variable-to-undefined) – aug Apr 12 '15 at 23:08
  • 1
    Different question about the same topic. – JSideris Apr 12 '15 at 23:09
  • 1
    `undefined` is just a data type/value in JS, similar to `null`. There can certainly be confusion between saying "the variable is undefined" and " the variable has the value undefined". – Felix Kling Apr 12 '15 at 23:19

1 Answers1

5

There's a difference between a variable being undeclared and being undefined:

var x;     //x is equal to *undefined*
alert(y);  //error, y is undeclared

This isn't self-contradicting, but it is redundant:

var x = undefined;

Think of undefined as simply the value a variable has when it hasn't been initialized – or the value an object property has when it hasn't been initialized or declared.

Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79
  • 3
    It's worth also noting that the *undefined* value is returned when accessing •undeclared* properties. – Jeremy Apr 13 '15 at 00:56