Skip to content

Add entry for JavaScript Array.prototype.entries() #7115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions content/javascript/concepts/arrays/terms/entries/entries.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
title: "Array.prototype.entries()"
slug: "javascript-array-entries"
category: "reference"
tags:
- javascript
- method
- arrays
---

## Description

The `entries()` method returns a new Array Iterator object that contains the key/value pairs for each index in the array. It allows you to loop through both the **index** and the **value** of each element in a structured way.

## Syntax

```javascript
array.entries()
```

## Parameters

This method does **not** take any parameters.

## Return Value

Returns a new Array Iterator object that can be used in a `for...of` loop or manually.

## Examples

```javascript
const cats = ['Peche', 'Moana', 'Pintassilga'];
const iterator = cats.entries();

for (let [index, name] of iterator) {
console.log(`Cat #${index}: ${name}`);
}

// Output:
// Cat #0: Peche
// Cat #1: Moana
// Cat #2: Pintassilga
```

## Codebyte

```codebyte/javascript
const fruits = ['apple', 'banana', 'cherry'];
const iterator = fruits.entries();

for (let [index, fruit] of iterator) {
console.log(index, fruit);
}
```

## Notes

- Often used with `for...of` loops.
- Helpful when both the index and value of elements are needed.
- The iterator object returned can also be converted using `Array.from()`.

## See Also

- [Array.prototype.keys()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keys)
- [Array.prototype.values()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values)
- [MDN Docs - entries()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries)