Horje
TypeScript Array.prototype.at() Method

The Array.prototype.at() method in TypeScript allows you to access elements in an array using both positive and negative indices. It returns the element stored at the specified index if the index is valid, otherwise it returns undefined.

Syntax:

array.at(index: number): T | undefined

Parameter:

  • index: It takes the index of the element whose value it will return.

Return Value:

  • Returns the element which is stored at the given index position. If the given index position is invalid then it will return undefined.

Examples of Array.prototype.at() Method

Example 1: Accessing Elements with Valid Indices

In this example, we will demonstrate how to access elements using both positive and negative indices.

Example 1: The below code will demonstrate that how to access elements by passing valid negative and positive both kinds of indices as parameters.

JavaScript
const arr: (number | string | boolean)[] = [1, 2, "TypeScript", false, true];

console.log(`Passed index is 1, will return 2nd element: ${arr.at(1)}`); 

console.log(`Passed index is -1, will return last value element: ${arr.at(-1)}`); 

console.log(`Passed index is 2, will return 3rd element: ${arr.at(2)}`); 

Output:

Passed index is 1, will return 2nd element: 2
Passed index is -1, will return last value element: true
Passed index is 2, will return 3rd element: TypeScript

Example 2: Handling Invalid Indices

In this example, we will show the behavior of the at() method when passing invalid index positions.

JavaScript
const arr: (number | string | boolean)[] = [1, 2, "TypeScript", false, true];

console.log(`Passed invalid index as -5: ${arr.at(-5)}`); 

console.log(`Passed invalid index as 6: ${arr.at(6)}`); 

console.log(`Passed invalid index as -7: ${arr.at(-7)}`);

Output:

Passed invalid index as -5: undefined
Passed invalid index as 6: undefined
Passed invalid index as -7: undefined

FAQs – Array.prototype.at() Method

What does the at() method return if the index is out of bounds?

If the index is out of bounds (either positive or negative), the at() method returns undefined.

Can the at() method be used with strings as well?

Yes, the at() method can also be used with strings to access characters at specified positions, similar to arrays.

What is the advantage of using the at() method over bracket notation?

The at() method provides a more concise and readable way to access elements, especially when using negative indices. It also avoids the need to compute the length of the array for accessing elements from the end.

Is the at() method supported in all JavaScript environments?

The at() method is part of the ECMAScript 2022 (ES13) specification. It is supported in most modern JavaScript environments, including the latest versions of browsers and Node.js. For older environments, you may need to use a polyfill.

How does the at() method handle negative indices?

The at() method interprets negative indices as counting from the end of the array. For example, at(-1) accesses the last element, at(-2) accesses the second last element, and so on.




Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to Convert Array of Objects into Unique Array of Objects in JavaScript ? How to Convert Array of Objects into Unique Array of Objects in JavaScript ?
Reverse a String in TypeScript Reverse a String in TypeScript
What is Memoization in JavaScript ? What is Memoization in JavaScript ?
What is Call in JavaScript ? What is Call in JavaScript ?
What is Asynchronous Code Execution ? What is Asynchronous Code Execution ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14