TypeScript Arrays

An array is a data structure that stores a collection of elements and are stored in contiguous memory locations.

example:

  • every array index position start with 0. so the first element value will be days[0] as shown in above example.
  • Similarly, it you want to fetch 2nd position element, the index will be 1 and the value will be days[1]

Table summarising some common TypeScript array methods and their descriptions:

MethodDescription
push(element1, element2, …, elementX)Adds one or more elements to the end of the array.
pop()Removes the last element of the array and returns it.
unshift(element1, element2, …, elementX)Adds one or more elements to the beginning of the array.
shift()Removes the first element of the array and returns it.
splice(start, deleteCount, item1, item2, …)Adds and/or removes elements from the array.
sort(compareFunction?)Sorts the elements of an array in place and returns the array. The compare function is an optional parameter that determines the sort order.
reverse()Reverses the elements of an array in place and returns the array.
concat(array2, array3, …, arrayX)Joins two or more arrays and returns a new array that contains all the elements.
slice(start, end?)Returns a shallow copy of a portion of an array into a new array object. The start and end parameters are optional and specify the index range to extract.
indexOf(searchElement, fromIndex?)Returns the first index at which a given element can be found in the array, or -1 if it is not present. The fromIndex parameter is optional and specifies the starting index for the search.

Note : these are just a few examples. TypeScript arrays have many more methods.

Example code for all array methods:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top