Nest all content into snippets

This commit is contained in:
Angelos Chalaris
2023-05-07 16:07:29 +03:00
parent 2ecadbada9
commit 6a45d2ec07
1240 changed files with 0 additions and 0 deletions

25
snippets/js/s/nth-arg.md Normal file
View File

@ -0,0 +1,25 @@
---
title: Nth argument
type: snippet
language: javascript
tags: [function]
cover: mug-flower-book
dateModified: 2020-10-21T21:54:53+03:00
---
Creates a function that gets the argument at index `n`.
- Use `Array.prototype.slice()` to get the desired argument at index `n`.
- If `n` is negative, the nth argument from the end is returned.
```js
const nthArg = n => (...args) => args.slice(n)[0];
```
```js
const third = nthArg(2);
third(1, 2, 3); // 3
third(1, 2); // undefined
const last = nthArg(-1);
last(1, 2, 3, 4, 5); // 5
```