Roger Stringer   About ▾

I'm Roger Stringer: a father, author, developer, consultant, chef, speaker. Founder of Data McFly.


Quick Function: Turn a Javascript Array Into a Human Readable String

Code

To take a JavaScript array like ["item1", "item2", "item3" ...] into a nice human readable string of item1, item2 & item3 with all the first items getting comma separated and the final one with an ampersand use this humanify (for lack of a better name) function as follows:

var items = ["item1","item2","item3","item4"];
			
			console.log( humanify( items ) );
			
			function humanify( array ){
			    if( array.length > 1 ){
			        return array.slice(0,-1).join(', ') + ' & ' + array[array.length -1];
			    }
			    return array[0];
			}