Reputation: 67
How can I return the first and last word from this array?
var array2 = ['milk','juice','lemonade','soda','water'];
I want it to return "milk-water" with the "-" combinding them.
Im not sure which of the array methods to use for this.
Upvotes: 0
Views: 75
Reputation: 104775
Use 0
for the first, and array.length - 1
for the last
var firstVal = array2[0];
var lastVal = array2[array2.length - 1]
var combo = firstVal + "-" + lastVal;
Upvotes: 4