Ticker

6/recent/ticker-posts

JavaScript - Array

JavaScript - Array

Introduction Arrays are a fundamental data structure in JavaScript that allows you to store and manipulate collections of values. They provide a way to organize and access data in a sequential manner. In this documentation, we will cover the basics of working with arrays in JavaScript, including how to create, access, modify, and iterate over arrays.

Table of Contents

  1. Creating an Array
  2. Accessing Array Elements
  3. Modifying Array Elements
  4. Array Methods
    • 4.1. push() and pop()
    • 4.2. shift() and unshift()
    • 4.3. splice()
    • 4.4. concat()
    • 4.5. slice()
    • 4.6. indexOf() and lastIndexOf()
    • 4.7. join()
    • 4.8. sort()
    • 4.9. reverse()
    • 4.10. forEach()
    • 4.11. map()
    • 4.12. filter()
    • 4.13. reduce()
  5. Multidimensional Arrays
  6. Conclusion

1. Creating an Array To create an array in JavaScript, you can use the array literal syntax, which involves enclosing a comma-separated list of values within square brackets ([]). For example:

javascript
let numbers = [1, 2, 3, 4, 5]; let fruits = ["apple", "banana", "orange"];

2. Accessing Array Elements You can access individual elements of an array using the square bracket notation combined with the index of the element. Array indices start from 0. For example:

javascript
let numbers = [1, 2, 3, 4, 5]; console.log(numbers[0]); // Output: 1 console.log(numbers[2]); // Output: 3

3. Modifying Array Elements Array elements can be modified by assigning new values to specific indices. For example:

javascript
let numbers = [1, 2, 3, 4, 5]; numbers[2] = 10; console.log(numbers); // Output: [1, 2, 10, 4, 5]

4. Array Methods JavaScript provides a variety of built-in methods for manipulating arrays. Here are some commonly used methods:

4.1. push() and pop()

  • The push() method adds one or more elements to the end of an array.
  • The pop() method removes the last element from an array.

4.2. shift() and unshift()

  • The shift() method removes the first element from an array.
  • The unshift() method adds one or more elements to the beginning of an array.

4.3. splice() The splice() method can be used to add or remove elements from an array at a specific index.

4.4. concat() The concat() method is used to merge two or more arrays together, returning a new array.

4.5. slice() The slice() method extracts a portion of an array and returns a new array.

4.6. indexOf() and lastIndexOf()

  • The indexOf() method returns the index of the first occurrence of a specified element in an array.
  • The lastIndexOf() method returns the index of the last occurrence of a specified element in an array.

4.7. join() The join() method combines all the elements of an array into a string.

4.8. sort() The sort() method sorts the elements of an array in place and returns the sorted array.

4.9. reverse() The reverse() method reverses the order of the elements in an array.

4.10. forEach() The forEach() method executes a provided function once for each array element.

4.11. map() The map() method creates a new array by applying a provided function to each element of the array.

4.12. filter() The filter() method creates a new array with all elements that pass a provided test.

4.13. reduce() The reduce() method applies a provided function to reduce the array to a single value.

5. Multidimensional Arrays JavaScript also supports multidimensional arrays, which are arrays of arrays. This allows you to create grids or matrices. For example:

javascript
let matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; console.log(matrix[1][2]); // Output: 6

6. Conclusion Arrays are a powerful data structure in JavaScript that provide flexibility and convenience for storing and manipulating collections of values. Understanding how to create, access, modify, and utilize array methods will enable you to work effectively with arrays in your JavaScript programs.

Post a Comment

0 Comments