Array in JavaScript

JavaScript is a scripting language that will allow you to add real programming to your webpage
  • 2252

Introduction: JavaScript is a scripting language that will allow you to add real programming to your webpageJavaScript is a client-side scripting language. This means the web surfer's browser will be running the script. JavaScript has been around for several years now in many different flavors. The main benefit ofJavaScript is to add additional interaction between the website and its visitors with just a little extra work by the web developer. JavaScript allows industrious web masters to get more out of their website than HTMLand CSS can provide and array is collection of similar type. JavaScript is most commonly used as a client side scripting language. This means that JavaScript code is written into an HTML page. When a user requests anHTML page with JavaScript in it the script is sent to the browser and it's up to the browser to do something with it.

Use of JavaScript:

  • Cookies
  • Control Browsers
  • Browser Detection
  • Validate Forms

JavaScript ArrayCreating an array is slightly different from creating a normal variable. Because JavaScripthas variables and properties associated with arrays, you have to use a special function to create a new array.:

  1. First way: This is the first way to declaration of array.
    var name=new Array(); 
    myCars[0]="manish";       
    myCars[1]="manu";
    myCars[2]="sonu";
  2. Second way: This is the second way to declaration of array.
    var  name=new Array("manish","manu","sonu");
  3. Theird Way: This is the Third way to declaration of array.
    var  name=["manish","manu","sonu"];

Example: The simple example of JavaScript in declare array is given below

Code:

     <html>
 
        <head>
 
            <title>Example of JavaScript </title>
 
            <script type="text/javascript">
 
                var arr = new Array(5);
                 arr[0] = "Manish";
                 arr[1] = "Sandeep";
                 arr[2] = "Amit";
                 arr[3] = "Rajesh";
                 arr[4] = "Arvind";
                 for (var i = 0; i < arr.length; i++) {
                     document.write("<b>arr[" + i + "] is </b>=>" + arr[i] + "<br>");
                 }
             </script
>
 
        </head>
 
        <body bgcolor="#ddcdff">
 
            <h2>
 
                This is my friends list
             </h2
>
 
        </body>
 
     </html>

Output: Open on browser

javascript.gif
 

Summary: This is the simple example that show how to declare array in a JavaScript. In a example we define a array of variable that name is "arr" and the size is five. In this example  we simply show friends name using a JavaScript.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.