Mark As Completed Discussion

Array methods are a set of built-in functions in JavaScript that allow you to manipulate arrays in various ways. These methods provide convenient ways to add, remove, or modify elements in an array.

One commonly used array method is push(). The push() method adds one or more elements to the end of an array and returns the new length of the array.

Here's an example:

JAVASCRIPT
1const myArray = [1, 2, 3];
2myArray.push(4);
3console.log(myArray); // Output: [1, 2, 3, 4]

In the above example, myArray.push(4) adds the number 4 to the end of the array myArray, resulting in the array [1, 2, 3, 4].

Another commonly used array method is pop(). The pop() method removes the last element from an array and returns that element.

Here's an example:

JAVASCRIPT
1const myArray = [1, 2, 3, 4];
2const lastElement = myArray.pop();
3console.log(lastElement); // Output: 4
4console.log(myArray); // Output: [1, 2, 3]

In the above example, myArray.pop() removes the last element (4) from the array myArray and returns it. The value of lastElement is then printed to the console, followed by the modified myArray without the last element.

These are just a few examples of the array methods available in JavaScript. Other commonly used array methods include shift(), unshift(), splice(), concat(), slice(), and forEach(). Each of these methods has its own specific purpose and can be a powerful tool when working with arrays.

By using array methods, you can efficiently manipulate arrays and perform common operations on them, making JavaScript programming more convenient and efficient.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment