Computer Science

Javascript Array Methods

Javascript Array Methods are built-in functions that can be used to manipulate arrays in Javascript. These methods include functions for adding, removing, and modifying elements in an array, as well as functions for sorting and searching arrays. Understanding these methods is essential for efficient and effective programming in Javascript.

Written by Perlego with AI-assistance

3 Key excerpts on "Javascript Array Methods"

  • Learning JavaScript Data Structures and Algorithms
    Arrays in JavaScript are modified objects, meaning that every array we create has a few methods available to be used. JavaScript arrays are very interesting because they are very powerful and have more capabilities available than primitive arrays in other languages. This means that we do not need to write basic capabilities ourselves, such as adding and removing elements in/from the middle of the data structure.
    The following is a list of the core available methods in an array object. We have covered some methods already:
    Method Description
    concat Joins multiple arrays and returns a copy of the joined arrays.
    every Iterates every element of the array, verifying the desired condition (function) until false is returned.
    filter Creates an array with each element that evaluates to true in the function provided.
    forEach Executes a specific function on each element of the array.
    join Joins all the array elements into a string.
    indexOf Searches the array for specific elements and returns its position.
    lastIndexOf Returns the position of the last item in the array that matches the search criterion.
    map Creates a new array from a function that contains the criterion/condition and returns the elements of the array that match the criterion.
    reverse Reverses the array so that the last item becomes the first and vice versa.
    slice Returns a new array from the specified index.
    some Iterates every element of the array, verifying the desired condition (function) until true is returned.
    sort Sorts the array alphabetically or by the supplied function.
    toString Returns the array as a string.
    valueOf Similar to the toString method, returns the array as a string.
     
    We have already covered the push, pop, shift, unshift, and splice methods. Let's take a look at these new ones. These methods will be very useful in the subsequent chapters of this book, where we will code our own data structure and algorithms. Some of these methods are very useful when we work with functional programming , which we will cover in
    Chapter 14 , Algorithm Designs and Techniques
  • JavaScript from Beginner to Professional
    • Laurence Lars Svekis, Maaike van Putten, Codestars By Rob Percival(Authors)
    • 2021(Publication Date)
    • Packt Publishing
      (Publisher)

    8

    Built-In JavaScript Methods

    We have just covered most of the basic building blocks in JavaScript. Now it's time to look at some powerful built-in methods that will make your life easier that we haven't seen yet. Built-in methods are functionality that we get out of the box with JavaScript. We can use these methods without having to code them first. This is something we have done a lot already, for example, console.log() and prompt() .
    Many built-in methods belong to built-in classes as well. These classes and their methods can be used at any time because JavaScript has already defined them. These classes exist for our convenience, since they are very common things to need, such as the Date , Array , and Object classes.
    The ability to harness the capabilities that are already built into JavaScript can improve the effectiveness of the code, save time, and comply with various best practices for developing solutions. We are going to address some of the common uses for such functions, such as manipulating text, mathematical computations, dealing with date and time values, interactions, and supporting robust code. Here are the topics covered in this chapter:
    • Global JavaScript methods
    • String methods
    • Math methods
    • Date methods
    • Array methods
    • Number methods
    Note: exercise, project and self-check quiz answers can be found in the Appendix .

    Introduction to built-in JavaScript methods

    We have seen many built-in JavaScript methods already. Any method that we didn't define ourselves is a built-in method. Some examples include console.log() , Math.random() , prompt() , and many more—think about methods on arrays for example. The difference between a method and a function is that a function is defined anywhere in the script, and a method is defined inside a class. So methods are pretty much functions on classes and instances.
    Methods can often be chained as well; this is only true for methods returning a result. The next method will then be performed on the result. So for example:
  • Beginning HTML and CSS
    • Rob Larsen(Author)
    • 2013(Publication Date)
    • Wrox
      (Publisher)
    Table 10-12 lists the common methods of an array.
    Table 10-12:
    Common Array Methods
    MethodPurpose
    concat() Joins (or concatenates) two or more arrays to create one new one.
    join(separator) Joins all the elements of an array together separated by the character specified as a separator. (The default is a comma.)
    pop() Removes the last element of an array and then returns the element.
    push() Adds an element to an array and returns the new array length.
    reverse() Returns the array with items in reverse order.
    shift() Removes the first element from an array and returns the element.
    slice() Returns a selected part of the array (if you do not need it all).
    sort() Returns a sorted array, sorted by alphabetical or numerical order.
    splice( index, howMany, [elements]) Returns a modified array. Modification starts at index and removes howMany elements from the array, replacing them with the elements provided as the third, fourth, fifth, etc. arguments.
    toString() Returns a string representing the array and its component elements.
    unshift() Adds one or more elements to the beginning of an array; returns the new length of an array.
    Table 10-13 lists array methods that are only available in modern browsers such as Internet Explorer 9 and other modern browsers such as Google Chrome and Mozilla Firefox.
    Table 10-13:
    New Array Methods
    MethodPurpose
    every() every() accepts a function as an argument. That function tests every element in the array and returns true if all pass.
    filter() filter() accepts a function as an argument. That function tests every element in the array and returns a new array containing every element for which the filtering function returns true.
    forEach() Calls a function for each element in the array.
    indexOf() Returns the first index of an element within the array that matches the specified value. Returns -1 if no match is found.
    lastIndexOf()
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.