web training room for online courses
Learn JavaScript, Free JavaScript Tutorial for Beginners, Interactive JavaScript Examples

JavaScript Array Syntax

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>
                

JavaScript Array Methods

push() method in array
When you want to add 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);

concat() method in array
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>");
}

sort() method in array
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] + "
"); }
reverse() method in array

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] + "
"); }

forEach() method in array
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);
    });
join() method in array

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>

indexOf() method in array

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
 
How to use JavaScript Array, Learn JavaScript Syntax

JavaScript Tutorial


Consulting