#atom
Content:
Arrays in JavaScript are ordered, indexed collections of values. They are a type of object and can hold multiple values of any data type, including numbers, strings, objects, and even other arrays. Arrays are dynamic in JavaScript, meaning their size can change at runtime.

Common Array Methods (with TypeScript Signatures):

  1. push(): Adds one or more elements to the end of an array.
    • Signature: push(...items: T[]): number;
  2. pop(): Removes and returns the last element of an array.
    • Signature: pop(): T | undefined;
  3. shift(): Removes and returns the first element of an array.
    • Signature: shift(): T | undefined;
  4. unshift(): Adds one or more elements to the beginning of an array.
    • Signature: unshift(...items: T[]): number;
  5. slice(): Returns a shallow copy of a portion of an array.
    • Signature: slice(start?: number, end?: number): T[];
  6. splice(): Adds or removes elements from an array at a specified index.
    • Signature: splice(start: number, deleteCount?: number, ...items: T[]): T[];
  7. concat(): Merges two or more arrays and returns a new array.
    • Signature: concat(...items: (T | ConcatArray<T>)[]): T[];
  8. forEach(): Executes a provided function once for each array element.
    • Signature: forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void;
  9. map(): Creates a new array with the results of calling a provided function on every element.
    • Signature: map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[];
  10. filter(): Creates a new array with all elements that pass a test implemented by a provided function.
    • Signature: filter(callbackfn: (value: T, index: number, array: T[]) => unknown, thisArg?: any): T[];
  11. reduce(): Executes a reducer function on each element, resulting in a single output value.
    • Signature: reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T;
  12. find(): Returns the first element in the array that satisfies a provided testing function.
    • Signature: find(callbackfn: (value: T, index: number, array: T[]) => unknown, thisArg?: any): T | undefined;
  13. indexOf(): Returns the first index at which a given element can be found, or -1 if not present.
    • Signature: indexOf(searchElement: T, fromIndex?: number): number;
  14. includes(): Determines whether an array includes a certain value.
    • Signature: includes(searchElement: T, fromIndex?: number): boolean;
  15. sort(): Sorts an array

Key Concepts:

Linked Cards:

Tags: #JavaScript #Arrays #DataStructures #Methods #Prototypes #TypeScript


Sources: