Introduction
Before reading this article, I highly recommend reading the following previous parts:
Arrays in JavaScript
It is nothing but multiple values in a single variable called an "Array". It can be an array of strings or numbers and so on. There is no trailing comma after the last value or element. Arrays are only special objects and with numbered indexes. It can be resized.
Syntax
Creating the array object, using the JavaScript keyword "new".
- 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”];
You can access the value using the array index starting from "0".
name [0] value is krishna.name [3] value is C# Corner.
Example
- <!DOCTYPE html>
- <html>
- <title>Arrays in JavaScript</title>
- <head>
- <script language='javascript'>
-
- var lang = new Array("C#","ASP.NET","JavaScript","MVC", "WCF");
-
- for(var i = 0; i < lang.length; i++)
- {
- document.write(lang[i] + "</br>")
- }
- </script>
- </head>
- <body>
- </body>
- </html>
Output
Properties and Methods in JavaScript
the following are the built-in JavaScript properties and methods.
Properties
- 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
An array is nothing but objects, you can delete an element from an array using the keyword "delete".
Syntax
- 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'>
-
- 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>");
- }
-
-
- document.write("</br>Length of an array is " + lang.length + "</br>");
-
-
- document.write("</br>Sorted Array is [" + lang.sort() + "]</br>");
-
-
- document.write("</br>Deleting the elements </br>");
- delete lang[0];
- for(var j = 0; j < lang.length; j++)
- {
- document.write(lang[j] + "</br>");
- }
-
-
- document.write("</br>Join Method [" + lang.join("*") + "]</br>");
-
- </script>
- </head>
- <body>
- </body>
- </html>
Output
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]
The preceding example clears the methods of an array.
Summary
I hope you understand the basic concepts of Arrays in JavaScript. If you have any suggestions regarding this article then please contact me.