Files
30-seconds-of-code/snippets/firstN.md
Angelos Chalaris 8a6b73bd0c Update covers
2023-02-16 22:24:28 +02:00

20 lines
404 B
Markdown

---
title: First n elements
tags: array
author: chalarangelo
cover: digital-nomad-16
firstSeen: 2022-07-22T05:00:00-04:00
---
Gets the first `n` elements of an array.
- Use `Array.prototype.slice()` with a start value of `0` and an end value of `n` to get the first `n` elements of `arr`.
```js
const firstN = (arr, n) => arr.slice(0, n);
```
```js
firstN(['a', 'b', 'c', 'd'], 2); // ['a', 'b']
```