Arrays in JavaScript are ordered, mutable collections of elements. They can hold different types of values, such as numbers, strings, objects, and even other arrays, and are used to store and manipulate data in a structured manner.
[]
and a comma-separated list of elements.javascriptlet fruits = ["apple", "banana", "cherry"];
Array
constructor.javascriptlet numbers = new Array(1, 2, 3, 4, 5);
javascriptconsole.log(fruits[0]); // "apple"
console.log(fruits[1]); // "banana"
console.log(fruits[2]); // "cherry"
javascriptfruits[0] = "orange";
console.log(fruits[0]); // "orange"
length
property returns the number of elements in an array.javascriptconsole.log(fruits.length); // 3
push
method adds an element to the end of an array, and the pop
method removes the last element from an array.javascriptfruits.push("grape");
console.log(fruits); // ["orange", "banana", "cherry", "grape"]
fruits.pop();
console.log(fruits); // ["orange", "banana", "cherry"]
shift
method removes the first element from an array, and the unshift
method adds an element to the beginning of an array.javascriptfruits.shift();
console.log(fruits); // ["banana", "cherry"]
fruits.unshift("apple");
console.log(fruits); // ["apple", "banana", "cherry"]
splice
method can be used to add, remove, or replace elements in an array. It takes three arguments: the starting index, the number of elements to remove, and the new elements to insert (if any).javascriptfruits.splice(1, 0, "orange");
console.log(fruits); // ["apple", "orange", "banana", "cherry"]
fruits.splice(1, 1);
console.log(fruits); // ["apple", "banana", "cherry"]
slice
method returns a new array containing a portion of the original array. It takes two arguments: the start index and the end index (not inclusive).javascriptlet newFruits = fruits.slice(1, 3);
console.log(newFruits); // ["banana", "cherry"]
indexOf
method returns the index of the first occurrence of an element in an array, and the lastIndexOf
method returns the index of the last occurrence. Both methods return -1 if the element is not found.javascriptconsole.log(fruits.indexOf("banana")); // 1
console.log(fruits.lastIndexOf("banana")); // 1
javascriptfruits.forEach(function (fruit) {
console.log(fruit);
}); // "apple", "banana", "cherry"
javascriptlet fruitLengths = fruits.map(function (fruit) {
return fruit.length;
});
console.log(fruitLengths); // [5, 6, 6]
javascriptlet longFruits = fruits.filter(function (fruit) {
return fruit.length > 5;
});
console.log(longFruits); // ["banana", "cherry"]
javascriptlet numbers = [1, 2, 3, 4, 5];
let sum = numbers.reduce(function (accumulator, currentValue) {
return accumulator + currentValue;
}, 0);
console.log(sum); // 15
some
method returns true if at least one element in the array passes a provided testing function, while the every
method returns true if all elements in the array pass the testing function.javascriptlet hasShortFruit = fruits.some(function (fruit) {
return fruit.length < 6;
});
console.log(hasShortFruit); // true
let allShortFruits = fruits.every(function (fruit) {
return fruit.length < 6;
});
console.log(allShortFruits); // false
concat
method is used to merge two or more arrays into a single array.javascriptlet vegetables = ["carrot", "potato", "onion"];
let food = fruits.concat(vegetables);
console.log(food); // ["apple", "banana", "cherry", "carrot", "potato", "onion"]
sort
method sorts the elements of an array in place, while the reverse
method reverses the order of elements in the array.javascriptfruits.sort();
console.log(fruits); // ["apple", "banana", "cherry"]
fruits.reverse();
console.log(fruits); // ["cherry", "banana", "apple"]
Arrays are a powerful and flexible data structure in JavaScript, allowing you to store and manipulate collections of elements. By understanding the various methods and properties of arrays, you can efficiently work with large datasets and create more sophisticated applications.