...

/

Hacks in ES5

Hacks in ES5

optional function arguments and their replacement, limitations of ES5 shortcuts

In some cases, function arguments are optional. For instance, let’s check the following code:

Node.js
function addCalendarEntry( event, date, len, timeout ) {
date = typeof date === 'undefined' ? new Date().getTime() : date;
len = typeof len === 'undefined' ? 60 : len;
timeout = typeof timeout === 'undefined' ? 1000 : timeout;
// ...
}
addCalendarEntry( 'meeting' );

Three arguments of addCalendarEntry are optional.

A popular shorthand for optional ...