Array length: Difference between revisions

Line 311:
<lang JavaScript>function last(list, defaultValue) {
return list.length ?list[list.length-1] :defaultValue;
}</lang>
 
Or use other built-in functions – this, for example, seems fairly clear, and is already 100+ times faster than unoptimised tail recursion in ES5 (testing with a list of 1000 elements):
 
<lang JavaScript>function last(list, defaultValue) {
return list.slice(-1)[0] || defaultValue;
}</lang>
 
9,659

edits