Skip to content

Instantly share code, notes, and snippets.

@akihiro
Created July 7, 2017 04:26
Show Gist options
  • Save akihiro/3daec38b24fdb09c47cd38acdbf915a7 to your computer and use it in GitHub Desktop.
Save akihiro/3daec38b24fdb09c47cd38acdbf915a7 to your computer and use it in GitHub Desktop.
default value on javascirpt
function defvalue(fn, v) {
let ret;
try{
ret = fn();
}catch(e){
ret = v;
}
if(ret === undefine){
ret = v;
}
return v;
}
// suppress TypeError: created_at of undefined is not defined
let date = defvalue(()=>res.body.foo.created_at, null);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment