JavaScript
Array
JavaScript arrays are a data type used to store a list of values. They can hold various data
types, including strings, numbers, Booleans, and even other arrays. Arrays are a special type
of object in JavaScript, and they are used to store multiple values in a single variable.
Example
const name = [“Rahul”, “Aman”, “Raj”];
const data = [12, “Akash”, false];
Accessing Array Elements
Array elements can be accessed using their index, which starts at 0. For example:
const arr = [1, 2, 3];
console.log(arr[0]);
Modifying Array Elements
Array elements can be modified by assigning a new value to their index. For example:
const arr = [1, 2, 3];
arr[0] = 10;
console.log(arr); // Output: [10, 2, 3]
Note: Array are mutable it can be change.
Array Methods
JavaScript arrays have several built-in methods that can be used to manipulate and
transform the array. Some common methods include:
• push(): Adds one or more elements to the end of the array.
• pop(): Removes the last element from the array.
• shift(): Removes the first element from the array.
• unshift(): Adds one or more elements to the beginning of the array.
• splice(): Removes or replaces elements in the array.
• slice(): Returns a subset of the array.
• concat(): Merges two or more arrays into a new array.
• join(): Converts the array into a string.
• forEach(): Executes a function for each element in the array.
• map(): Creates a new array with the results of applying a function to each element in
the array.
• filter(): Creates a new array with the elements that pass a test implemented by a
function.
• reduce(): Applies a function against an accumulator and each element in the array
(from left to right) to reduce it to a single value.