Free Web Development Online Tutorials, Learn to Code
Learn JavaScript Array, javascript array methods, push, sort, slice, add, indexof example

JavaScript Array Methods

In this tutorial you will learn how to work with Javascript Array

JavaScript Array Iteration, JavaScript array object, JavaScript Array Methods, JavaScript Arrays tips, tricks and examples

What is 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
<script>
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

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

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

array.find(function(element))
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];
    arrNumbers.push(60,70,80,90);

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

var subjects = ["JavaScript", "JSON", "Asp.Net Core", "PHP"];
    subjects.forEach(function (fetch) {
        document.writeln(fetch);
    });
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

<script> 
var subjects = ["JavaScript", "JSON", "Asp.Net Core", "PHP"];
var subjects2 = subjects.join();
document.write(subjects2);
//using separators
var subjects3 = subjects.join('*');
document.write(subjects3);
</script>
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

<script>
var array = [10, 20, 30, 10,20];
var position = array.indexOf(20);
document.writeln(position);
// result will be 1
var position = array.indexOf(20, 2);
document.writeln(position);
// result will be 4
</script>
Comment
Name
Email
Website
Subscribe