Menu OmegaForms.Net

JavaScript: Arrays

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.

  1. Creating Arrays:
  • Array literal: The most common way to create an array is by using an array literal, which consists of square brackets [] and a comma-separated list of elements.
javascript
let fruits = ["apple", "banana", "cherry"];
  • Array constructor: Alternatively, you can create an array using the Array constructor.
javascript
let numbers = new Array(1, 2, 3, 4, 5);
  1. Accessing Elements:
  • Elements in an array are accessed using their index (position) in the array. The index is zero-based, meaning the first element has an index of 0, the second element has an index of 1, and so on.
javascript
console.log(fruits[0]); // "apple" console.log(fruits[1]); // "banana" console.log(fruits[2]); // "cherry"
  1. Modifying Elements:
  • You can modify the value of an array element by assigning a new value to the element at the specified index.
javascript
fruits[0] = "orange"; console.log(fruits[0]); // "orange"
  1. Array Properties and Methods:
  • Length: The length property returns the number of elements in an array.
javascript
console.log(fruits.length); // 3
  • Push and Pop: The push method adds an element to the end of an array, and the pop method removes the last element from an array.
javascript
fruits.push("grape"); console.log(fruits); // ["orange", "banana", "cherry", "grape"] fruits.pop(); console.log(fruits); // ["orange", "banana", "cherry"]
  • Shift and Unshift: The shift method removes the first element from an array, and the unshift method adds an element to the beginning of an array.
javascript
fruits.shift(); console.log(fruits); // ["banana", "cherry"] fruits.unshift("apple"); console.log(fruits); // ["apple", "banana", "cherry"]
  • Splice: The 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).
javascript
fruits.splice(1, 0, "orange"); console.log(fruits); // ["apple", "orange", "banana", "cherry"] fruits.splice(1, 1); console.log(fruits); // ["apple", "banana", "cherry"]
  • Slice: The 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).
javascript
let newFruits = fruits.slice(1, 3); console.log(newFruits); // ["banana", "cherry"]
  • indexOf and lastIndexOf: The 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.
javascript
console.log(fruits.indexOf("banana")); // 1 console.log(fruits.lastIndexOf("banana")); // 1
  • forEach: This method executes a provided function once for each array element. It does not return a new array.
javascript
fruits.forEach(function (fruit) { console.log(fruit); }); // "apple", "banana", "cherry"
  • map: This method creates a new array by applying a provided function to every element in the original array.
javascript
let fruitLengths = fruits.map(function (fruit) { return fruit.length; }); console.log(fruitLengths); // [5, 6, 6]
  • filter: This method creates a new array containing the elements that satisfy a provided testing function.
javascript
let longFruits = fruits.filter(function (fruit) { return fruit.length > 5; }); console.log(longFruits); // ["banana", "cherry"]
  • reduce: This method applies a provided function (accumulator) to the array elements, reducing them to a single value.
javascript
let numbers = [1, 2, 3, 4, 5]; let sum = numbers.reduce(function (accumulator, currentValue) { return accumulator + currentValue; }, 0); console.log(sum); // 15
  • Some and Every: The 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.
javascript
let 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: The concat method is used to merge two or more arrays into a single array.
javascript
let vegetables = ["carrot", "potato", "onion"]; let food = fruits.concat(vegetables); console.log(food); // ["apple", "banana", "cherry", "carrot", "potato", "onion"]
  • Sort and Reverse: The sort method sorts the elements of an array in place, while the reverse method reverses the order of elements in the array.
javascript
fruits.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.