1

After reading JavaScript Garden, I took notes as below but is it possible prove number 2. Please validate & correct my other proofs also.

  1. undefined is a type.
  2. undefined type has only one value. that values is undefined.
  3. There is a global variable called undefined
  4. global undefined variable has default value undefined
  5. global undefined variable is not a constant
  6. undefined is not a keyword

Proof (tested in firebug console)

1. typeof undefined \\undefined 
 2. Need proof
 3. var name = undefined;  \\ global
    variable undefined is assigned to
    name
 4. alert(name); \\ undefined, that
    means undefined global variable has
    value undefined
 5. undefined = 'rajakvk';  \\ we can
    change value
 6. var undefined = 'rajakvk';  \\ no
    syntax error

var if = 'rajakvk';  \\ SyntaxError: missing variable name

Thanks http://w3fools.com/#what-should-be-done

rajakvk
  • 9,775
  • 17
  • 46
  • 49

2 Answers2

2

You can prove it by looking at the language spec. There is no test that you can run in an interpreter that will prove it. Such a test would only be evidence that the interpreter is or is not conforming with the spec. The spec is what defines "undefined."

Section 8.1 of the language spec says

8.1 The Undefined Type

The Undefined type has exactly one value, called undefined. Any variable that has not been assigned a value has the value undefined.

And there is exactly one tyoe for which typeof will return "undefined". From section 11.4.3 of the spec:

Table 20 — typeof Operator Results

Type of val                                          Result
Undefined                                            "undefined"
Null                                                 "object"
Boolean                                              "boolean"
Number                                               "number"
String                                               "string"
Object (native and does not implement [[Call]])      "object"
Object (native or host and does implement [[Call]])  "function"
Object (host and does not implement [[Call]])        Implementation-defined except may not be "undefined", "boolean", "number", or "string".
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
1

The correct proof is:

undefined = 1;
alert(typeof undefined !== 'undefined'); // need to be true

It need to be true, because you've changed the undefined variable value to anything different from undefined type.

Welington Veiga
  • 151
  • 1
  • 7
  • ehr, but now typeof undefined is 'number', right? Funny stuff: try `undefined = [1,2,3,4,5]; alert(undefined[2]);` – KooiInc Apr 26 '11 at 16:32
  • also, what to make of : `var c=1; undefined = 1; alert(c === undefined); //=>true` – KooiInc Apr 26 '11 at 16:40
  • You're confusing the undefined value, that returned by the `void` operator as in `void 0`, with the value of the global variabled named `undefined`. – Mike Samuel Apr 26 '11 at 19:34