Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created October 24, 2011 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboy/1309769 to your computer and use it in GitHub Desktop.
Save cowboy/1309769 to your computer and use it in GitHub Desktop.
Testing to see if an argument has been omitted
function joinArray(arr, separator) {
// if (typeof separator === "undefined") {
// if (separator === undefined) {
// if (arguments.length === 1) {
if (separator == null) {
separator = " - ";
}
return arr.join(separator);
}
joinArray([1, 2, 3]) // "1 - 2 - 3"
joinArray([1, 2, 3], ", ") // "1, 2, 3"
joinArray([1, 2, 3], "") // "123"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment