JavaScript Arrays and Array Methods Documentation
Arrays in JavaScript are used to store multiple values in a single variable. They are a special type of object that allows you to work with ordered collections of data.
Creating Arrays
Arrays can be created using array literals or the Array
constructor.
Example Code
let arr = [1, 2, 3, 4, 5, 6]; // Array literal
arr[3] = 8; // Modifying the value at index 3
Explanation
arr
is an array containing six elements. The value at index 3 is modified to 8.
Array Methods
JavaScript provides various methods to manipulate arrays. Here are some commonly used methods:
Example Code
let fastFood = ["Maggie", "Momos", "Burger"]; // Array of fast food items
console.log(fastFood); // Outputs: ["Maggie", "Momos", "Burger"]
console.log(fastFood.indexOf("Momos")); // Outputs: 1 (index of "Momos")
console.log(fastFood.includes("Pizza")); // Outputs: false (checks if "Pizza" is in the array)
fastFood.push("Pizza"); // Adds "Pizza" to the end of the array
fastFood.unshift("Fried Momos"); // Adds "Fried Momos" to the beginning of the array
fastFood.pop(); // Removes the last element ("Pizza")
fastFood.shift(); // Removes the first element ("Fried Momos")
console.log(fastFood.length); // Outputs: 2 (length of the array)
Explanation
indexOf("Momos")
returns the index of the first occurrence of "Momos" in the array.includes("Pizza")
checks if "Pizza" is present in the array and returnsfalse
.push("Pizza")
adds "Pizza" to the end of the array.unshift("Fried Momos")
adds "Fried Momos" to the beginning of the array.pop()
removes the last element from the array.shift()
removes the first element from the array.length
property returns the number of elements in the array.
Combining Arrays
You can combine arrays using various methods, including the spread operator and the concat()
method.
Example Code
let homeFood = ["Roti", "Sabji", "Salad"];
// let arr2 = fastFood + homeFood; // Concatenation using + operator
// fastFood.push(homeFood); // Adds the entire home Food array as a single element
// let arr2 = fastFood.concat(homeFood); // Concatenation using concat method
let arr2 = [...fastFood, "You are Coder", ...homeFood, ...arr]; // Using spread operator to combine arrays
Explanation
- The spread operator (
...
) allows you to expand elements of an array into another array, making it easy to combine multiple arrays and add additional elements.
Array Slicing and Splicing
JavaScript provides methods to extract or remove elements from arrays.
Example Code
let arr3 = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
// let tukda = arr3.slice(2, 6); // Extracts elements from index 2 to 5
let tukda = arr3.splice(2, 4); // Removes 4 elements starting from index 2
console.log(tukda); // Outputs the removed elements
console.log(arr3); // Outputs the modified array
console.log(arr3.reverse()); // Reverses the order of elements in the array
Explanation
slice(start, end)
returns a shallow copy of a portion of an array into a new array object, without modifying the original array.splice(start, deleteCount)
changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.reverse()
reverses the elements of the array in place.
Creating and Filling Arrays
You can create arrays of a specific length and fill them with a value.
Example Code
let arr4 = new Array(9).fill(null); // Creates an array of length 9 filled with null
console.log(arr4); // Outputs: [null, null, null, null, null, null, null, null, null]
Explanation
new Array(length)
creates an array with the specified length, andfill(value)
fills all elements of the array with the specified value.
Joining Array Elements
You can join all elements of an array into a string using the join()
method.
Example Code
let joined = arr3.join("."); // Joins elements of arr3 with a dot separator
console.log(joined); // Outputs: "10.20.30.40.50.60.70.80.90.100"
Explanation
join(separator)
creates and returns a new string by concatenating all of the elements in an array, separated by the specified separator.
Conclusion
Arrays are a fundamental part of JavaScript, allowing you to store and manipulate collections of data efficiently. Understanding array methods is essential for effective programming in JavaScript.