The best way to do this is
var args = { bing: "bing", bang: myVar, boom: "boom" } if (myVar === undefined) { delete args.myVar; } someFunc(args, "yay");
It is clear that adding it when it exists is better and then removing it when it does not exist.
The bottom line is to handle something like this, really not, itโs safer to use native delete to remove a property from an object.
You can crack it with setTimeout and callbacks. I recommend repeating this
var args = { bing: 4, bang: (myVar === undefined) ? (function() { setTimeout(function() { delete args.bang; }, 0); })() : myVar }, "yay");
source share