Array length: Difference between revisions
→or other, more directly relevant built-in functions
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>
|