Skip to content

Instantly share code, notes, and snippets.

@AlbinSoft
Last active January 17, 2024 16:36
Show Gist options
  • Save AlbinSoft/4f3582d25d899e28a8077e644306c8a6 to your computer and use it in GitHub Desktop.
Save AlbinSoft/4f3582d25d899e28a8077e644306c8a6 to your computer and use it in GitHub Desktop.
Join array strings in a human readable way
function strjoin(a) {
if(a.length===0) return '';
let r = a.shift();
while(a.length>1) {
r += ', '+a.shift();
}
if(a.length) {
r += ' and '+a.shift();
}
return r;
}
/*
'I like '+strjoin(['apples', 'bananas', 'cherries']);
I like apple, bananas and cherries
Demo: https://codepen.io/albinsoft/pen/jOJBxOJ?editors=0010
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment