Arrays in JavaScript: Day 5

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".

  1. var numbers = new Array (1,2,3,4);  
You can create an array and simply assign values as in the following:
  1. 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
  1. <!DOCTYPE html>  
  2. <html>  
  3. <title>Arrays in JavaScript</title>  
  4. <head>  
  5. <script language='javascript'>  
  6.     //Creating the array object  
  7.     var lang = new Array("C#","ASP.NET","JavaScript","MVC""WCF");  
  8.     //Printing array of objects  
  9.     for(var i = 0; i < lang.length; i++)  
  10.     {  
  11.         document.write(lang[i] + "</br>")  
  12.     }  
  13. </script>  
  14. </head>  
  15. <body>  
  16. </body>  
  17. </html>  
Output 

    C#
    ASP.NET
    JavaScript
    MVC
    WCF

Properties and Methods in JavaScript

the following are the built-in JavaScript properties and methods.

Properties:

  1. Index: Its represent zero based index.
  2. Length: It finds length of array in other words numbers of elements in array.
  3. Prototype: it allows you to add methods and properties to object.

Methods:

  1. join(): It joins all elements into a string.
  2. sort(): It sorts elements of an array.
  3. reverse(): It reverses the order of elements in array.
  4. pop(): It removes the last element from the array.
  5. push(): It adds the element to an array (at the end).
  6. shift(): It removes the first element from the array.
  7. 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

  1. var d = new Array(“Jeet”,”krishna”);  
  2. delete d[0];  
The following example shows the properties and methods used in Array:
  1. <!DOCTYPE html>  
  2. <html>  
  3. <title>Arrays in JavaScript</title>  
  4. <head>  
  5. <script language='javascript'>  
  6.     //Creating the array object  
  7.     document.write("</br> Creating an Array </br>");  
  8.     var lang = new Array("C#","ASP.NET","JavaScript","MVC""WCF");  
  9.     for(var i = 0; i < lang.length; i++)  
  10.     {  
  11.         document.write(lang[i] + "</br>");  
  12.     }  
  13.       
  14.     //Length of an Array  
  15.     document.write("</br>Length of an array is " + lang.length + "</br>");  
  16.       
  17.     //Sorting an Array  
  18.     document.write("</br>Sorted Array is [" + lang.sort() + "]</br>");  
  19.       
  20.     //Deleting an Array element  
  21.     document.write("</br>Deleting the elements </br>");  
  22.     delete lang[0];  
  23.     for(var j = 0; j < lang.length; j++)  
  24.     {  
  25.         document.write(lang[j] + "</br>");      
  26.     }  
  27.       
  28.     //join() method  
  29.     document.write("</br>Join Method [" + lang.join("*") + "]</br>");  
  30.       
  31. </script>  
  32. </head>  
  33. <body>  
  34. </body>  
  35. </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 suggestion regarding this article then please contact me.