/ Published in: JavaScript

Simple way of allowing functions to take optional arguments. Always place the optional arguments last, so you don't have to explicitly pass an argument as "undefined".
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function optionalArguments(a, /*optional*/b){ if(!b){b = []};//If b isn't set, create an empty array //OR shorthand for this b = b || []; /* * The OR operator looks to see if b is true, if it is it skips * creating the Array. If b is false it creates the array. Not * very readable. */ }
Comments
