Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created June 13, 2012 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwaldron/2926029 to your computer and use it in GitHub Desktop.
Save rwaldron/2926029 to your computer and use it in GitHub Desktop.
null is not undefined.
// http://traceur-compiler.googlecode.com/git/demo/repl.html
// open the console
// paste this into the text area.
//
// or
//
// Run in Firefox 15 (Nightly)
function nullIsNotUndefined( arg = "foo" ) {
return arg;
}
console.log( nullIsNotUndefined() === "foo" );
console.log( nullIsNotUndefined(null) === null );
Copy link

ghost commented Jun 13, 2012

Not at my computer at the moment (and running Traceur on an iPod touch is an unenviable experience), but what does nullIsNotUndefined(undefined) return? In other words, do default parameters check for undefined, or arguments.length?

@rwaldron
Copy link
Author

They check for only undefined; so in the first case arg is undefined so therefore is assigned "foo". In the second case, null is a valid value and therefore accepted as if it were defined intentionally.

Copy link

ghost commented Jun 13, 2012

Hmm...that seems a bit inconsistent (String() === ""; String(undefined) === "undefined"), but I guess it makes sense.

Thank you!

@espadrine
Copy link

@rwldrn: in the current Firefox implementation, nullIsNotUndefined(undefined) returns undefined.

The proposal says: "If no actual parameter was passed", then use the default parameter.

http://wiki.ecmascript.org/doku.php?id=harmony:parameter_default_values

Of course, that is something that Traceur would have a hard time implementing.

@rwaldron
Copy link
Author

That makes sense though, because it's explicit and there would be something at arguments[0], right? undefined is a thing:

15.1.1.3 undefined
The value of undefined is undefined (see 8.1). This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

...

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.

...

4.3.9
undefined value
primitive value used when a variable has not been assigned a value.

...

4.3.10
Undefined type
type whose sole value is the undefined value.

Unrelated: I had tested this in FF15 - which I thought was the latest Nightly, I was wrong, the latest Nightly is 16x

@espadrine
Copy link

It does make sense, but it also means that the difference between undefined and null is only semantic.

(That, and the fact that you cannot assign null.)

@rwaldron
Copy link
Author

Technically, undefined isn't writable or configurable either, but unfortunately implementation adherence isn't consistent

@espadrine
Copy link

The end result is, undefined and null have been spec'ed as having very similar behavior. Their main difference is in their meaning. The standard library uses null very sparingly, but quite frankly each of its use could be replaced by undefined without losing anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment