Skip to main content

nthArg

TS JS Deno

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

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

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