Files
30-seconds-of-code/snippets/nthArg.md
Angelos Chalaris bac4e5d9c1 Add nthArgument
2018-01-23 21:27:37 +02:00

383 B

nthArg

Creates a function that gets the argument at index n. If n is negative, the nth argument from the end is returned.

Use Array.slice() to get the desired argument at index n.

const nthArg = n => (...args) => args.slice(n)[0];
const third = nthArg(2);
third(1,2,3); // 3
third(1,2); // undefined
const last = nthArg(-1);
last(1,2,3,4,5); // 5