- Source:
Interface: Nothing
Nothing
A marker 'interface' that represents anything convertible to false
in a boolean expression1. More specifically, all of the
following 'implement' Nothing
:
false
0
(The number 0)NaN
''
(The empty string)null
undefined
(Any undefined value2)
Anything that is not convertible to false
(i.e. not one of the above values) does not implement this interface.
Throughout this documentation, this interface is often used to indicate a return value from a function (typically a callback) is optional, and
specific actions will be taken in the event that function returns a value convertible to false
. To exemplify the concept, both the following functions
are considered to return Nothing
:
// Literally returns nothing (though a call to it will implicitly return undefined).
function foo() {
'use strict';
}
// Returns a value, but that value is convertible to false.
// (undefined is technically returned in this case, but any of the values specified act to the same effect)
function bar() {
'use strict';
return false || 0 || NaN || '' || null || undefined;
}
1More colloquially, this interface is referred to as 'falsy'.
MDN has an article defining the term in more detail.
2
undefined
is not actually a reserved word, but a built-in variable defined on the Global Object,
which implies it can be hidden in a local scope. Another widely recognized, yet 'maximally portable' way of specifying a literal undefined value is
the expression void 0
. See MDN's
article on the subject for more details.