Online Training for Asp.net MVC Web Designing Development, MS SQL, Digital Marketing

JavaScript Arrays

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");


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>



Comment
Name Email Website
Subscribe
 
JavaScript Arrays

JavaScript Tutorial

Group Training