JavaScript Array Methods Example

In this tutorial you will learn how to work with Javascript Arrays!

To work with array we need to know JavaScript Array Iteration, JavaScript array object, JavaScript Array Methods etc, let's learn JavaScript Arrays tips, tricks with examples.

javascript array

How to Create JavaScript Array?

An array is a special variable, can store multiple values in it.
Or you can say Array is a collection object of same type of data, or can be of different type of data, Array is very useful when we want to store big number of same type data and post or retrieve them in one call.

Here we learn different aspect of working with usage of JavaScript array

Simple way of creating JavaScript Array using new keyword.
var i;
var subjects = new Array(); //Creating array
subjects[0] = "JavaScript"; subjects[1] = "JSON"; subjects[2] = "PHP"; subjects[3] = "Asp.Net Core"; // Retrieving values from array for (i = 0; i < subjects.length; i++) { document.write(subjects[i] + "<br>"); } </script>

Store comma separated values in array example.

var subjects = ["JavaScript", "JSON", "Asp.Net Core", "PHP"];
for (i = 0; i < subjects.length; i++) {
                document.write(subjects[i] + "<br>");

If you want to get array length, you simply print subjects.length, means array variable .length

JavaScript Array Example

javascript Array Contains following methods

Javascript array find method

Array.find is an inbuilt function in JavaScript, Returns a value if element found in that array

var memberAge = [13, 15, 18, 21];
var isValidAge = memberAge.find(function(age) {
return age >= 18;
// you also can write this way
function checkAge(age) {
return age >= 18;
var isValidAge = memberAge.find(checkAge)
javascript array push method

When you want to add element to javascript array, a new item in existing array, you can use push method. for example you want to add a new subject in above subjects array.

var subjects = ["JavaScript", "JSON", "Asp.Net Core", "PHP"];
subjects.push("SQL Database");
var arrNumbers = [10, 20, 30, 40];

Javascript array concat method

Concat method combines two or more arrays and returns a new array();

var subjects = ["JavaScript", "JSON", "Asp.Net Core", "PHP"];
var subjects1 = ["Java", "Python", "LINQ", "XML"];
var subjects2 = subjects.concat(subjects1);
for (i = 0; i < subjects2.length; i++) {
document.writeln(subjects2[i] + "<br>");

Javascript array sort method

sort method is used to arrange the array elements in ascending order.

var subjects = ["JavaScript", "JSON", "Asp.Net Core", "PHP"];
var subjects2 = subjects.sort();
for (i = 0; i < subjects2.length; i++) {
document.writeln(subjects2[i] + "
"); }
Javascript array reverse method

reverse method changes the sequence of elements in array and returns the reverse sequence

var subjects = ["JavaScript", "JSON", "Asp.Net Core", "PHP"];
var subjects2 = subjects.reverse();
for (i = 0; i < subjects2.length; i++) {
document.writeln(subjects2[i] + "
"); }
Javascript array forEach method

forEach() method is used to invoke each item from specified array, we can call forEach in different ways

var subjects = ["JavaScript", "JSON", "Asp.Net Core", "PHP"];
subjects.forEach(function (fetch) {

// Or we can call a function like below
function fetch(item, index) {
    console.log(index + ":" + item );

Javascript array join method

join method combines all the array elements into a string, you also can specify any separators, like any character you want to use in between

var subjects = ["JavaScript", "JSON", "Asp.Net Core", "PHP"];
var subjects2 = subjects.join();
//using separators
var subjects3 = subjects.join('*');
Javascript array indexOf method

indexOf function is used for finding the index of the first occurrence of the any element (argument) in an Array.
indexOf will get the index position of search item, in following example we are finding the position of number 20

var array = [10, 20, 30, 10,20];
var position = array.indexOf(20);
// result will be 1
var position = array.indexOf(20, 2);
// result will be 4
Does JavaScript support two-dimensional array?

No, by default there is no way you can create two-dimensional array directly, however, you can create a two-dimensional array using logic like example below.

let countries = [];

countries = [


Here is how the output will look like!

│ (index) │      0      │     1      │
│    0    │   'India'   │  'Delhi'   │
│    1    │ 'Australia' │ 'Canebera' │
│    2    │    'UK'     │  'London'  │
│    3    │  'Canada'   │ 'Toranto'  │

You may be interested to know how array is initiated in other programming language, look at following posts.

Advanced JavaScript
Javascript Array Function Examples
javascript Interview Questions Answers
JavaScript Examples | JavaScript Online Course