<!-- !
Array Methods -->
JavaScript provides a wide range of built-in methods to manipulate and work
with arrays. Here are some commonly used array methods:
1. `push()`
- Purpose: Adds one or more elements to the end of the array.
- Returns: The new length of the array.
let numbers = [1, 2, 3];
[Link](4); // Adds 4 to the end of the array
[Link](numbers); // Output: [1, 2, 3, 4]
2. `pop()`
- Purpose: Removes the last element from the array.
- Returns: The removed element.
let fruits = ["Apple", "Banana", "Cherry"];
let lastFruit = [Link](); // Removes the last element "Cherry"
[Link](fruits); // Output: ["Apple", "Banana"]
[Link](lastFruit); // Output: Cherry
3. `shift()`
- Purpose: Removes the first element from the array.
- Returns: The removed element.
let fruits = ["Apple", "Banana", "Cherry"];
let firstFruit = [Link](); // Removes "Apple"
[Link](fruits); // Output: ["Banana", "Cherry"]
[Link](firstFruit); // Output: Apple
4. `unshift()`
- Purpose: Adds one or more elements to the beginning of the array.
- Returns: The new length of the array.
let fruits = ["Banana", "Cherry"];
[Link]("Apple"); // Adds "Apple" to the beginning
[Link](fruits); // Output: ["Apple", "Banana", "Cherry"]
5. `indexOf()`
Definition:
The `indexOf()` method returns the first index at which a specified element is
found in the array. If the element is not found, it returns `-1`.
Syntax:
[Link](searchElement, fromIndex);
Example:
let fruits = ["Apple", "Banana", "Cherry", "Banana"];
let index = [Link]("Banana"); // Finds the first occurrence
[Link](index); // Output: 1
6. `lastIndexOf()`
Definition:
The `lastIndexOf()` method returns the last index at which a specified element
is found in the array. If the element is not found, it returns `-1`.
Syntax:
[Link](searchElement, fromIndex);
Example:
let fruits = ["Apple", "Banana", "Cherry", "Banana"];
let lastIndex = [Link]("Banana"); // Finds the last occurrence
[Link](lastIndex); // Output: 3
7. `concat()`
- Purpose: Merges two or more arrays into a new array.
- Returns: A new array.
let arr1 = [1, 2];
let arr2 = [3, 4];
let mergedArray = [Link](arr2);
[Link](mergedArray); // Output: [1, 2, 3, 4]
8. `includes()`
- Purpose: Determines whether an array contains a certain value.
- Returns: `true` if the array contains the value, otherwise `false`.
let fruits = ["Apple", "Banana", "Cherry"];
[Link]([Link]("Banana")); // Output: true
10. `reverse()`
Definition:
The `reverse()` method reverses the order of the elements in an array in
place. The first array element becomes the last, and the last becomes th e
first.
Syntax:
[Link]();
- The `reverse()` method directly modifies the original array.
- It does not create a new array, but rather changes the order of elements in
the array itself.
Example:
let numbers = [1, 2, 3, 4, 5];
[Link](); // Reverses the order of the array
[Link](numbers); // Output: [5, 4, 3, 2, 1]
Key Points:
- In-Place Modification: The original array is modified.
11. `join()`
Definition:
The `join()` method joins all elements of an array into a string, with a
specified separator between the elements. If no separator is provided, a comma
(`,`) is used by default.
Syntax:
[Link](separator);
- `separator` (optional): Specifies the string to separate each element. If
omitted, a comma is used.
Example:
let fruits = ["Apple", "Banana", "Cherry"];
let joinedFruits = [Link](" - ");
[Link](joinedFruits); // Output: "Apple - Banana - Cherry"
12. `slice()`
- Purpose: Extracts a section of an array and returns a new array.
- Returns: A new array containing the extracted elements.
let fruits = ["Apple", "Banana", "Cherry", "Date"];
let slicedFruits = [Link](1, 3); // Extracts elements at index 1 and 2
[Link](slicedFruits); // Output: ["Banana", "Cherry"]
13. `splice()`
- Purpose: Adds or removes elements from the array.
- Returns: An array containing the deleted elements.
let fruits = ["Apple", "Banana", "Cherry"];
[Link](1, 1, "Mango"); // Replaces "Banana" with "Mango"
[Link](fruits); // Output: ["Apple", "Mango", "Cherry"]
14. `[Link]()`
Definition:
The `[Link]()` method determines whether the passed value is an array.
Syntax:
[Link](value);
- value: The value you want to check.
Returns: `true` if the value is an array; otherwise, `false`.
Example:
[Link]([Link]([1, 2, 3])); // Output: true
[Link]([Link]("Hello")); // Output: false
[Link]([Link]({ key: "value" })); // Output: false
15. `flat()`
Definition:
The `flat()` method creates a new array with all sub-array elements
concatenated into it recursively up to the specified depth.
Syntax:
[Link](depth);
- depth (optional): Specifies how deep to flatten the array. Default is `1`.
Example:
let nestedArray = [1, [2, [3, [4]]]];
[Link]([Link](2)); // Output: [1, 2, 3, [4]]
// ! how to declare array
let arr1 = [10,'san',true,[20,40],()=>{}]
[Link](arr1[4])
// ! how to know the length of array
let len = [Link]
[Link](len)
// ! methods of array
let arr2 = [20,40,60,33,60,12]
// ! 1. push()
// let lenForPush = [Link](99)
// [Link](lenForPush)
// [Link](arr2)
// ! 2. pop()
// let lastEle = [Link]()
// [Link](lastEle)
// [Link](arr2)
// ! 3. unshift()
// let lenForUnshift = [Link]('hi')
// [Link](lenForUnshift)
// [Link](arr2)
// ! 4. shift()
// let removedEleFromStart = [Link]()
// [Link](removedEleFromStart)
// [Link](arr2)
// ! 5. includes()
[Link]([Link](60))
// ! 6. indexOf()
[Link]([Link](60))
// ! 7. lastIndexOf()
[Link]([Link](60))
// ! 8. concat()
let arr3 = [3,4,5,6]
let arr4 = [8,9,6,5]
let newArr = [Link](arr4)
[Link](newArr)
// ! 9. flat()
let arr5 = [1,3,4,[67,90,90,[3,4,5,['hi','hello']]]]
[Link](arr5)
let flatArr = [Link](Infinity)
[Link](flatArr)
// ! 10. reverse()
let arr6 = [ 4,6,8,9,23]
[Link]()
[Link](arr6)
// ! 11. join()
let str = [Link]("")
[Link](str)
// ! 12. slice()
let arr7 = [3,6,7,8,9,20]
let slicedArr = [Link](1,4)
[Link](slicedArr) // 6,7,8
// ! 13. splice()
// [Link](1,3,'hi')
// [Link](arr7)
[Link](1,0,'hi') // it will add hi at the index 1
[Link](arr7)
// ! 14 [Link]()
[Link]([Link](arr7))
[Link]([Link]("hi"))