TypeScript Array.prototype.at() Method Last Updated : 19 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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 | undefinedParameter: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() MethodExample 1: Accessing Elements with Valid IndicesIn 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: TypeScriptExample 2: Handling Invalid IndicesIn 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 Comment More infoAdvertise with us Next Article TypeScript Array.prototype.at() Method pankajbind Follow Improve Article Tags : JavaScript Web Technologies TypeScript Similar Reads TypeScript Array.of() Method The Array.of() method in TypeScript is used to create a new array from a variable number of arguments, regardless of the data types of those arguments. This inbuilt function provides a straightforward way to create arrays with the specified elements.SyntaxArray.of(items)Parametersitems: Here you can 2 min read TypeScript Object The Array Type In TypeScript, the Array Type is used to specify and enforce the type of elements that can be stored in an array, enhancing type safety during development. This adaptability ensures reusability across diverse data types, as exemplified by the Array type (e.g., number[] or string[]). Syntaxlet myArra 2 min read Array filter() Method - TypeScript The Array.filter() method in TypeScript creates a new array with elements that pass the test provided by a callback function. It does not modify the original array and accepts an optional thisObject for context within the callback function.Syntaxarray.filter(callback[, thisObject])Parameter: This me 3 min read TypeScript Array entries() Method The Array.prototype.entries() method in TypeScript returns a new array iterator object that contains the key/value pairs for each index in the array. This method is useful when you need to iterate over the key/value pairs in the array.Note: In TypeScript, the entries() method is not available direct 2 min read TypeScript Array.from() Method The Array.from() method in TypeScript converts array-like or iterable objects into actual arrays. It allows the creation of arrays from objects with a length property or iterable structures, optionally applying a map function to each element during the conversion.SyntaxArray.from(object, mapFunction 2 min read TypeScript Array Symbol.iterator Method In TypeScript the Symbol.iterator function plays a role, in allowing iteration over arrays using for...of loops and other iterator-based mechanisms. This built-in function eliminates the need, for definition when working with arrays simplifying the process of accessing elements. Syntax:const iterato 3 min read TypeScript Array lastIndexOf() Method The Array.prototype.lastIndexOf() method in TypeScript is used to find the last index at which a given element can be found in the array, searching backwards from the fromIndex. It returns the index of the found element, or -1 if the element is not present.Syntax:array.lastIndexOf(searchElement[, fr 2 min read TypeScript Array indexOf() Method The Array.indexOf() method in TypeScript is used to find the index of the first occurrence of a specified element in an array. If the element is not found, it returns -1.Syntaxarray.indexOf(searchElement[, fromIndex])Parameter: This method accepts two parameters as mentioned above and described belo 2 min read JavaScript TypedArray.prototype.with() Method A new method TypedArray.prototype.with(), was introduced in ECMAScript 2023 (ES2023) to provide a means of modifying an element in a particular TypedArray without altering the underlying array directly, it generates a new TypedArray with the required alteration and then returns it. TypedArray instan 2 min read JavaScript Error.prototype.toString() Method The Error.prototype.toString() method is an inbuilt method in JavaScript that is used to return a string representing the specified Error object. Syntax: e.toString() Parameters: This method does not accept any parameters. Return value: This method returns a string representing the specified Error o 1 min read Like