Array Methods in JavaScript

Description:

Array methods in javascript- in this article, I am going to show you how to use array methods and properties with programming examples.


Array Properties and methods in JavaScript:

JavaScript considers an array as an object. An object is a collection of properties and methods. The length or size of an array represents the property. Methods of an object are use to access the data(read or modify data) from the object.

length property in JavaScript:

The length property in javaScript is used to find the length or size of an array. For example, to find the length of an array ‘abc’, the statement is written as;

The size of the array abc will be assigned to the variable ‘s’

For example, if an array is declared as;

The statement “document.write(arr.length);” will display 15 in the browser window.



Array Methods in JavaScript:

The most important and commonly used methods in JavaScript associated with array objects are as follows.

Concat method in javaScript:

The word concat stands for concatenate, which means combine. The concat method in javascript is used to combine two arrays. It creates new array and assigns the contents of two combine arrays to new created array.

General syntax of concat method in javaScript:

Where var1 and var2 are two arrays that you want to combine together. The combined data of two arrays will be assigned to new created array var3.


Example1: how to use concat method in javascript programming:

For example, to combine the contents of two arrays var1 and var2 into new created array var3, javascript code is given as follows:

output:

methods in JavaScript

Join method in JavaScript:

join method in javascript combines all values of elements of an array into a string and returns a string.


General syntax of join method in javascript:

Where separator specifies the character that will be displayed between the contents of the elements of array. If separator is not specifies, then comma(,) will be displayed by default as a separator.

example2: how use join method in javascript:

Output:

methods in JavaScript


Reverse method in javascript:

reverse method in javascript is used to reverse the contents of elements in an array.

General syntax of reverse method in javascript:

Example3:  how to use reverse method in javascrpt:

Output:

methods in JavaScript



Sort method in javascript:

 sort method in javascript is used to sort the contents of an array.

General syntax of sort method in javascript:

Exmaple3: how to use sort method in javascript:

Output:

methods in JavaScript


toString method in javascript:

toString method in javascript is used to convert the content of an array into string. The data of different elements of an array are separated by commas.

General syntax toString method in javascript:

Example4: how to use toString method in javascript:

Output:

methods in JavaScript


Related Articles:

JavaScript Form validation: Checking Empty, Numeric, Alphabetic values

Date Objects in JavaScript Properties and Methods with Examples

 

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: