4 ms·
> For example, the last item in an array: arr[arr.length - 1] Of course, one could argue that this just means that the length of an array is a less meanin
by JadeNB 10y ago
> For example, the last item in an array:
arr[arr.length - 1]
Of course, one could argue that this just means that the length of an array is a less meaningful property than its last populated index (what Perl calls `$#array`); one can imagine
arr[arr.lastIndex]
or, which is probably better, just
arr.last
> Or a found item by index number:
if (arr.indexOf("thing") > -1) {
This comes from overloading return values to communicate errors. It's not clear to me that
if (arr.indexOf("thing") > 0) {
(in a hypothetical analogue with 1-indexed lists) demonstrates any clear improvement; something like
if (arr.member?("thing")) {
seems preferable in terms of clarity (ignoring the efficiency objection that one would probably immediately after look for the index).