JavaScript Array Methods


1. concat(): Returns a new array object by concatenating two or more arrays.

Syntax:

    array1.concat(array2, array3,... , arrayN);


Example:

    var site1 = [“Tutorials”, “Duniya”];
	var site2 = [“Apni”, “Duniya”];
	var website = site1.concat(site2);


2. fill(): Returns the modified array after filling the elements with specified static values.

Syntax:

    arrayName.fill(value, start, end);


Example:

    var website = [“Tutorials”, “Duniya”];
	website.fill(“Domain”, 1,1);
	document.write(website); // prints Tutorials Domain


3. filter(): Returns a new array filled with all array elements that pass a test.

Syntax:

    arrayName.filter(func);


Example:

    var age = [18, 22, 31, 19];
    function check(value)
    {
        value > 20;
    }
    document.write(age.filter(check)); // prints 22,31    


4. find(): Returns the value of the first element in an array that pass a test. If the element is not found, then it returns undefined.

Syntax:

    arrayName.find(func);


Example:

    var age = [18, 22, 31, 19];
    function check(value)
    {
        value > 20;
    }
    document.write(age.find(check)); // prints 22    


5. findIndex(): Returns the index of the first element in an array that pass a test. If the element is not found then it returns -1.

Syntax:

    arrayName.findIndex(func);


Example:

    var age = [18, 22, 31, 19];
    function check(value)
    {
        value > 20;
    }
    document.write(age.findIndex(check)); // prints 1    



6. forEach(): Invokes the specified function once for each array element in order.

Syntax:

    arrayName.forEach(func);


Example:

    var age = [18, 22, 31, 19];
    age.forEach(function(age)
    {
        document.write(age);
    });


7. includes(): Returns true if the array includes the specified element else returns false.

Syntax:

    arrayName.includes(element);


Example:

    var age = [18, 22, 31, 19];
	document.write(age.includes(31));


8. indexOf(): Returns the index of first occurrence of the specified element. If the element is not found, it returns -1.

Syntax:

    arrayName.indexOf(element);


Example:

    var age = [18, 22, 31, 19, 31];
	document.write(age.indexOf(31)); // prints 2


9. isArray(): Returns true if the object is an array else returns false.

Syntax:

    arrayName.isArray(object);


Example:

    var age = [18, 22, 31, 19];
	document.write(age.isArray());


10. join(): Returns a string after joining elements of an array into a string. The elements will be separated by a specified separator. The default separator is comma (,).

Syntax:

    arrayName.join(separator);


Example:

    var website = [“My”, “website”, “is”, “TutorialsDuniya”];
	document.write(website.join(“ “); prints My website is TutorialsDuniya



11. lastIndexOf(): Returns the index of last occurrence of the specified element. If the element is not found, it returns -1.

Syntax:

    arrayName.lastIndexOf(element);


Example:

    var age = [18, 22, 31, 19, 31];
	document.write(age.lastIndexOf(31)); // prints 4


12. push(): Returns the array after adding the specified elements at the end of the array.

Syntax:

    arrayName.push(element1, element2,..., element);


Example:

    var age = [18, 22, 31];
	document.write(age.push(51,46)); // prints 18 22 31 51 46


13. pop(): Returns and removes the last element of the array.

Syntax:

    arrayName.pop();


Example:

    var age = [18, 22, 31];
	document.write(age.pop()); // prints 31


14. reverse(): Returns the array with elements in reverse order.

Syntax:

    arrayName.reverse();


Example:

    var age = [18, 22, 31];
	document.write(age.reverse()); // prints 31 22 18


15. unshift(): Returns the array after adding the specified elements at the beginning of the array.

Syntax:

    arrayName.unshift(element1, element2,..., element);


Example:

    var age = [18, 22, 31];
	document.write(age.unshift(51,46)); // prints 51 46 18 22 31



16. shift(): Returns and removes the first element of the array.

Syntax:

    arrayName.shift();


Example:

    var age = [18, 22, 31];
	document.write(age.shift()); // prints 18


17. slice(): Returns a new array object with selected elements only.

Syntax:

    arrayName.slice(start, end);


Example:

    var website = [“my”, “website”, “is”, “TutorialsDuniya”, “com”];
	document.write(website.slice(1, 4)); // prints website is TutorialsDuniya


18. sort(): Returns the sorted array object.

Syntax:

    arrayName.sort();


Example:

    var website = [“my”, “website”, “is”, “tutorialsduniya”, “com”];
	document.write(website.sort()); // prints com is my tutorialsduniya website


19. splice(): It adds/removes items to/from the given array and returns the removed item(s).

Syntax:

    arrayName.splice(index, number of items to be removed, items);


Example:

    var website = [“my”, “website”, “is”, “tutorialsduniya”, “com”];
	website.splice(1,1,”site”); // Now my site is tutorialsduniya com


20. toString(): Returns a string of all array elements separated with commas(,).

Syntax:

    arrayName.toString();


Example:

    var website = [“my”, “website”, “is”, “tutorialsduniya”, “com”];
	document.write(website.toString()); // prints my,website,is,tutorialsduniya,com