Skip to content

Instantly share code, notes, and snippets.

@Canop
Last active August 29, 2015 14:01
Show Gist options
  • Save Canop/0b931ca60cbb0afc09cd to your computer and use it in GitHub Desktop.
Save Canop/0b931ca60cbb0afc09cd to your computer and use it in GitHub Desktop.
Proposal for some sugar for default values in JavaScript

To deal with default argument value, we often use :

a = a || defaultValue;

or when a falsy value can be a valid argument :

if (a===undefined) a = defaultValue;

I think we could have some helpful sugar here, by which the last LOC would be written

a \= defaultValue;

An advantage is that it would limit the profusion of buggy a = a || defaultValue;.

\ would also be used as operator in expressions :

(a\defaultValue);

which would be similar to

(a!==undefined ? a : defaultValue)

(defaultValue not being evaluated if a is defined)

A possible coherent extension would be the definition of default literal values in a function definition :

function fun(a\defaultValue, b\someOtherDefault, c){
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment