0% found this document useful (0 votes)
3 views4 pages

JavaScript Array and String Methods Reference

Uploaded by

tusharshinde2030
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

JavaScript Array and String Methods Reference

Uploaded by

tusharshinde2030
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

# JavaScript Array and String Methods Reference

## Array Methods

### Creating and Modifying Arrays

| Method | Description | Example |

|--------|-------------|---------|

| `push()` | Adds one or more elements to the end of an array | `arr.push('new item')` |

| `pop()` | Removes the last element from an array | `const lastItem = arr.pop()` |

| `unshift()` | Adds one or more elements to the beginning of an array | `arr.unshift('first item')` |

| `shift()` | Removes the first element from an array | `const firstItem = arr.shift()` |

| `splice()` | Changes array content by removing/replacing elements | `arr.splice(1, 2, 'new')` |

| `slice()` | Returns a shallow copy of a portion of an array | `const newArr = arr.slice(1, 3)` |

| `concat()` | Combines two or more arrays | `const combined = arr1.concat(arr2)` |

| `fill()` | Fills all elements with a static value | `arr.fill(0)` |

### Array Iteration Methods

| Method | Description | Example |

|--------|-------------|---------|

| `forEach()` | Executes a function for each array element | `arr.forEach(item => console.log(item))` |

| `map()` | Creates a new array with results of calling a function | `const doubled = arr.map(x => x *
2)` |

| `filter()` | Creates a new array with elements that pass a test | `const evens = arr.filter(x => x % 2
=== 0)` |

| `reduce()` | Reduces array to single value by applying function | `const sum = arr.reduce((total, x)
=> total + x, 0)` |

| `find()` | Returns first element that passes a test | `const found = arr.find(x => x > 10)` |

| `findIndex()` | Returns index of first element that passes a test | `const index = arr.findIndex(x => x
> 5)` |

| `some()` | Tests if at least one element passes a test | `const hasLarge = arr.some(x => x > 100)` |
| `every()` | Tests if all elements pass a test | `const allPositive = arr.every(x => x > 0)` |

### Sorting and Searching

| Method | Description | Example |

|--------|-------------|---------|

| `sort()` | Sorts elements of an array | `arr.sort((a, b) => a - b)` |

| `reverse()` | Reverses the order of elements | `arr.reverse()` |

| `indexOf()` | Returns first index at which element is found | `const index = arr.indexOf(5)` |

| `lastIndexOf()` | Returns last index at which element is found | `const lastIndex = arr.lastIndexOf(5)`
|

| `includes()` | Determines if array includes element | `const has7 = arr.includes(7)` |

### Other Array Methods

| Method | Description | Example |

|--------|-------------|---------|

| `join()` | Joins all elements into a string | `const str = arr.join(', ')` |

| `flat()` | Creates new array with sub-array elements flattened | `const flat = arr.flat()` |

| `flatMap()` | Maps each element, then flattens the result | `const flatMapped = arr.flatMap(x => [x,
x * 2])` |

| `from()` | Creates a new array from an array-like object | `const arr = Array.from('hello')` |

| `isArray()` | Checks if a value is an array | `Array.isArray(arr)` |

## String Methods

### Basic String Methods

| Method | Description | Example |

|--------|-------------|---------|

| `charAt()` | Returns character at specified index | `str.charAt(0)` |

| `charCodeAt()` | Returns UTF-16 code at specified index | `str.charCodeAt(0)` |


| `concat()` | Joins two or more strings | `str1.concat(str2)` |

| `startsWith()` | Checks if string begins with specified characters | `str.startsWith('hello')` |

| `endsWith()` | Checks if string ends with specified characters | `str.endsWith('.com')` |

| `includes()` | Checks if string contains specified characters | `str.includes('world')` |

| `indexOf()` | Returns first index of specified text | `str.indexOf('a')` |

| `lastIndexOf()` | Returns last index of specified text | `str.lastIndexOf('a')` |

### String Manipulation

| Method | Description | Example |

|--------|-------------|---------|

| `replace()` | Replaces specified value with another value | `str.replace('old', 'new')` |

| `replaceAll()` | Replaces all occurrences of a value | `str.replaceAll('a', 'b')` |

| `slice()` | Extracts part of a string | `str.slice(1, 5)` |

| `substring()` | Extracts characters between two indices | `str.substring(1, 5)` |

| `split()` | Splits string into an array of substrings | `str.split(' ')` |

| `toLowerCase()` | Converts string to lowercase | `str.toLowerCase()` |

| `toUpperCase()` | Converts string to uppercase | `str.toUpperCase()` |

| `trim()` | Removes whitespace from both ends | `str.trim()` |

| `trimStart()` | Removes whitespace from beginning | `str.trimStart()` |

| `trimEnd()` | Removes whitespace from end | `str.trimEnd()` |

| `padStart()` | Pads start of string to specified length | `str.padStart(10, '0')` |

| `padEnd()` | Pads end of string to specified length | `str.padEnd(10, '0')` |

### String Search and Match

| Method | Description | Example |

|--------|-------------|---------|

| `match()` | Searches string for match against regex | `str.match(/pattern/g)` |

| `matchAll()` | Returns iterator of all regex matches | `[...str.matchAll(/pattern/g)]` |

| `search()` | Searches for match against regex | `str.search(/pattern/)` |


### Property

| Property | Description | Example |

|----------|-------------|---------|

| `length` | Returns the length of a string | `str.length` |

You might also like