Arrays in JavaScript are used to store multiple values in a single variable. They are a fundamental aspect of managing collections of data.
- Length: Determines the number of elements in an array.
const arr = [1, 2, 3]; console.log(arr.length); // Output: 3
-
push(): Adds one or more elements to the end of an array and returns the new length.
arr.push(4); // Adds 4 to the end
-
unshift(): Adds one or more elements to the beginning of an array and returns the new length.
arr.unshift(0); // Adds 0 to the start
-
pop(): Removes the last element from an array and returns that element.
arr.pop(); // Removes the last element
-
shift(): Removes the first element from an array and returns that element.
arr.shift(); // Removes the first element
This guide covers the basics of array manipulation in JavaScript, focusing on commonly used methods and properties. Understanding these will significantly aid in data handling and manipulation tasks in your projects. Practice these methods to become proficient in managing arrays efficiently.
Happy coding! 😊