No announcement yet.

Javascript Arrays


  • JavaScript
    started a blog post Javascript Arrays

    Javascript Arrays

    This blog will discuss arrays in javascript:
    1. Overview of Arrays
    2. Creating Arrays and Accessing Items
    3. Examples of Creating Array and Initialization
    4. Destructuring and Rest
    5. Examples of Destructuring and Rest
    6. Adding, Removing, Copying, and Sorting Items
    7. Iterating Over Array Items
    8. Examples of Iterating Over Array Items
    9. Array Iterables
    10. Examples of keys, values, and Entries

    Overview of Arrays

    Arrays in JavaScript provide a structure for storing a collection of data and functions for manipulating the collection of data.
    const colors = [ 'red', 'yellow', 'blue', 'orange', green' ]
    Arrays to access data elements, indexes are being used.
    The first index is zero because indexing is zero-based.
    Like the rest of JavaScript, arrays are not strongly typed, they can hold any mix of data types

    const colors = ['color',123]
    Arrays are sparse, they can contain data at index O, 1, and 10 with the indexes between 1 and 10 being empty which means they return a value of undefined
    Using square brackets to an array index does not signify an object is an array
    To determine if an object is an array two methods can be used

    Method 1:

    colors instanceof Array => true
    Array.isArray(colors) => true

    Array as Objects
    Arrays are objects which inherit from the Array prototype
    The Array prototype itself inherits from the top-level Object provided by JavaScript
    The Array prototype provides useful functions for working with Arrays
    Ultimately, Array indexes and their values are simply Object properties
    The indexes themselves are not really numbers, they are ultimately strings like every other property

    Creating Arrays and Accessing Items

    arrays were created with array constructor

    const arr = new Array(); // empty array
    const arr = new Array(5); // empty 5 slots
    const arr = new Array('1','2')
    Using Array literals

    const arr = ['1','c']
    Array Object Methods:

    Methods are functions in JavaScript, but are used at times to refer to a function when the word function would be confused with something else
    In addition to the Array constructor and the array literal syntax, the constructor methods from and of can be used as well
    The from method (function) takes an array-like object or an iterable and returns a true array object
    An example of an array-like structure would be the arguments object, or a node list of DOM objects
    The of method (function) takes a list of arguments, and produces an array object of those arguments
    The of function serves as an alternative to the array literal syntax for creating an array initialized with values
      You must be logged in to post a comment.

    Related Topics