Skip to content

Instantly share code, notes, and snippets.

@amitkumar
Created September 4, 2014 22:12
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 amitkumar/076c15c6d66273b066e1 to your computer and use it in GitHub Desktop.
Save amitkumar/076c15c6d66273b066e1 to your computer and use it in GitHub Desktop.
Join an array of words with commas and a final "and"
function joinWithCommas (array){
var result = '';
$.each(array, function(index, label){
if (index === 0){
} else if (index < (array.length - 1)){
result += ', ';
} else {
result += ' and ';
}
result += label;
});
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment