#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):
push()
: Adds one or more elements to the end of an array.- Signature:
push(...items: T[]): number;
- Signature:
pop()
: Removes and returns the last element of an array.- Signature:
pop(): T | undefined;
- Signature:
shift()
: Removes and returns the first element of an array.- Signature:
shift(): T | undefined;
- Signature:
unshift()
: Adds one or more elements to the beginning of an array.- Signature:
unshift(...items: T[]): number;
- Signature:
slice()
: Returns a shallow copy of a portion of an array.- Signature:
slice(start?: number, end?: number): T[];
- Signature:
splice()
: Adds or removes elements from an array at a specified index.- Signature:
splice(start: number, deleteCount?: number, ...items: T[]): T[];
- Signature:
concat()
: Merges two or more arrays and returns a new array.- Signature:
concat(...items: (T | ConcatArray<T>)[]): T[];
- Signature:
forEach()
: Executes a provided function once for each array element.- Signature:
forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void;
- Signature:
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[];
- Signature:
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[];
- Signature:
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;
- Signature:
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;
- Signature:
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;
- Signature:
includes()
: Determines whether an array includes a certain value.- Signature:
includes(searchElement: T, fromIndex?: number): boolean;
- Signature:
sort()
: Sorts an array
Key Concepts:
- Arrays are zero-indexed.
- Arrays are mutable, meaning their contents can be changed after creation.
- Arrays inherit methods and properties from
Array.prototype
.
Linked Cards:
- Data Types in JavaScript: Arrays are a composite data type in JavaScript.
- Functions in JavaScript: Many array methods (
forEach
,map
,filter
, etc.) rely on callback functions. - Prototypes in JavaScript: Arrays inherit methods from
Array.prototype
. - Classes in JavaScript: Arrays can be used within classes, and classes can have methods that manipulate arrays.
- Syntactic Sugar: Some array methods (e.g.,
map
,filter
) provide syntactic sugar for common operations. - How to randomly sort arrays in javascript
Tags: #JavaScript #Arrays #DataStructures #Methods #Prototypes #TypeScript
Sources: