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
Related key terms
1 of 5
3 Key excerpts on "Javascript Array Methods"
- No longer available |Learn more
- Loiane Groner(Author)
- 2018(Publication Date)
- Packt Publishing(Publisher)
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 inChapter 14 , Algorithm Designs and Techniques - eBook - ePub
JavaScript from Beginner to Professional
Learn JavaScript quickly by building fun, interactive, and dynamic web apps, games, and pages
- Laurence Lars Svekis, Maaike van Putten, Rob Percival, Laurence Svekis, 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: - eBook - PDF
- Chris Minnick(Author)
- 2023(Publication Date)
- For Dummies(Publisher)
Although it’s possible to accomplish everything that the built-in array functions can do by using various loops, operators, and conditional statements, knowing how to use the most important array functions makes your life easier. Table 6-1 lists the most commonly used array functions, along with descriptions of what they do or the values they produce. TABLE 6-1 Javascript Array Methods Method Return Value concat() A new array made up of the current array, joined with other arrays and/or values every() True if every element in the given array satisfies the provided testing function filter() A new array with all the elements of a current array that test true by the given function forEach() Completes the function once for each element in the array includes() Determines whether an array includes a specified value and returns true or false indexOf() Finds the first occurrence of the specified value within the array; returns –1 if the value is not found join() Joins all elements of an array into a string lastIndexOf() Finds the last occurrence of the specified value within the array; returns –1 if the value is not found map() Creates a new array with the result of a provided function on every element in the array pop() Removes the last element in an array push() Adds new items to the end of an array reduce() Reduces the values in an array to a single value by applying a function to them (from left to right) Using Arrays CHAPTER 6 Using Arrays 113 In the following sections, I explain and demonstrate the array methods that you’re most likely to encounter or need. If you want to follow along in your JavaScript console, start by creating an array to work with. I’ll use the following array, con- taining the ingredients for a frittata, for all examples in this section: const ingredients = ['eggs','milk','cheese','garlic','onion','kale','salt', 'pepper']; Pushing and popping No, they’re not dance moves.
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.


