Introduction
- Introduction to JavaScript: Day 1
- Programming Basics in JavaScript: Day 2
- Comments and Objects in JavaScript: Day 3
- Scope and Events in JavaScript: Day 4
Arrays in JavaScript
- var numbers = new Array (1,2,3,4);
You can create an array and simply assign values as in the following:
- var name = [“krishna”,”jeetendra”,”radha”,”C# Corner”,”JavaScript”];
name [3] value is C# Corner.
Example
- <!DOCTYPE html>
- <html>
- <title>Arrays in JavaScript</title>
- <head>
- <script language='javascript'>
- //Creating the array object
- var lang = new Array("C#","ASP.NET","JavaScript","MVC", "WCF");
- //Printing array of objects
- for(var i = 0; i < lang.length; i++)
- {
- document.write(lang[i] + "</br>")
- }
- </script>
- </head>
- <body>
- </body>
- </html>
C#
ASP.NET
JavaScript
MVC
WCF
ASP.NET
JavaScript
MVC
WCF
Properties and Methods in JavaScript
the following are the built-in JavaScript properties and methods.- Index- It represents the zero-based index.
- Length- It finds the length of the array in other words numbers of elements in the array.
- Prototype- it allows you to add methods and properties to object.
Methods
- join()- It joins all elements into a string.
- sort()- It sorts elements of an array.
- reverse()- It reverses the order of elements in the array.
- pop()- It removes the last element from the array.
- push()- It adds the element to an array (at the end).
- shift()- It removes the first element from the array.
- unshift()- It adds the new element to the array (at the beginning).
Delete in JavaScript
- var d = new Array(“Jeet”,”krishna”);
- delete d[0];
The following example shows the properties and methods used in Array:
- <!DOCTYPE html>
- <html>
- <title>Arrays in JavaScript</title>
- <head>
- <script language='javascript'>
- //Creating the array object
- document.write("</br> Creating an Array </br>");
- var lang = new Array("C#","ASP.NET","JavaScript","MVC", "WCF");
- for(var i = 0; i < lang.length; i++)
- {
- document.write(lang[i] + "</br>");
- }
- //Length of an Array
- document.write("</br>Length of an array is " + lang.length + "</br>");
- //Sorting an Array
- document.write("</br>Sorted Array is [" + lang.sort() + "]</br>");
- //Deleting an Array element
- document.write("</br>Deleting the elements </br>");
- delete lang[0];
- for(var j = 0; j < lang.length; j++)
- {
- document.write(lang[j] + "</br>");
- }
- //join() method
- document.write("</br>Join Method [" + lang.join("*") + "]</br>");
- </script>
- </head>
- <body>
- </body>
- </html>
Creating an Array
C#
ASP.NET
JavaScript
MVC
WCF
Length of an array is 5
Sorted Array is [ASP.NET,C#,JavaScript,MVC,WCF]
Deleting the elements
undefined
C#
JavaScript
MVC
WCF
Join Method [*C#*JavaScript*MVC*WCF]
C#
ASP.NET
JavaScript
MVC
WCF
undefined
C#
JavaScript
MVC
WCF
The preceding example clears the methods of an array.
I hope you understand the basic concepts of Arrays in JavaScript. If you have any suggestions regarding this article then please contact me.

Jitendra KumarPosted Dec 17, 2014, 11:40 AM
good..
Praveen KumarPosted Dec 16, 2014, 11:09 PM
It's too good! could you tell me what does #ff0000 mean in output of Delete elements
Vithal WadjePosted Dec 16, 2014, 10:03 PM
good work