Javascript-array-method

JavaScript Array Methods

Converting Arrays to Strings

The JavaScript method toString() converts an array to a string of (comma separated) array values.

Example

const fruits = ["Banana""Orange""Apple""Mango"];
let fruitsString = fruits.toString();
console.log(fruitsString);
Result:

Banana,Orange,Apple,Mango

The join() method also joins all array elements into a string.

It behaves just like toString(), but in addition you can specify the separator:

Example

const fruits = ["Banana""Orange""Apple""Mango"];
let fruitsJoin = fruits.join(" * ");
console.log(fruitsJoin);
Result:

Banana * Orange * Apple * Mango

Popping and Pushing

When you work with arrays, it is easy to remove elements and add new elements.

This is what popping and pushing is:

Popping items out of an array, or pushing items into an array.

JavaScript Array pop()

The pop() method removes the last element from an array:

Example

const fruits = ["Banana""Orange""Apple""Mango"];
fruits.pop();

Result:

["Banana","Orange","Apple"]

The pop() method returns the value that was “popped out”:

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

four − 1 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top