---
title: "Array in java script"  
description: "Array is collection of similar type of elements. We can use concept of array to make program more readable, easy to handle large data and implementing"  
author: "Anonymous User"  
published: 2011-03-09  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/506/array-in-java-script  
category: "javascript"  
tags: ["javascript"]  
reading_time: 2 minutes  

---

# Array in java script

Array is [collection](https://www.mindstick.com/articles/1718/collections-in-java) of similar type of elements. We can use concept of array to make [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) more readable, easy to [handle large](https://www.mindstick.com/forum/161330/what-are-some-efficient-ways-to-handle-large-input-data-in-python) data and implementing [concept of loops](https://answers.mindstick.com/qa/111558/can-you-explain-the-concept-of-loops) in program. With the help of array the size of code is also decrease. When we allocate memory to [objects](https://www.mindstick.com/forum/159749/what-are-data-transfer-objects-dtos-and-why-are-they-used-in-api-development) in array then allocation of element in memory is continuous. Array is used in static [data structure](https://www.mindstick.com/articles/332406/why-it-is-important-to-learn-data-structure-and-algorithm) to represents data in memory. In this demonstration we learn how to [implement array](https://www.mindstick.com/interview/23091/how-to-implement-array-in-c-plus-plus-plus-plus-using-function) in java script.\

##### Creating an array object

We can use new Array () method to create an array object in java script like following statement creates an array object.\

```
var fruitCollection = new Array();
```

##### Initializing values in Array Object

Here we can use [ ] to initialize values in array object like following example demonstrate.fruitCollection = ["Mango", "Pine Apple", "Apple", "Orange"];

An example which demonstrate use of Array Object

```
<html xmlns="http://www.w3.org/1999/xhtml"><head>     <title></title>     <script type="text/javascript">        function arrayDemo() {            //Creating an array object.            var fruitCollection = new Array();            //Initilizing value to an array object.            fruitCollection = ["Mango", "Pine Apple",  "Apple", "Orange"];            //Accessing value of fruitCollection array            document.write("First Value In FruitCollection  :  " + fruitCollection[0] + "<br />");            document.write("Next Value In FruitCollection  :  " + fruitCollection[1] + "<br />");            document.write("Next Value In FruitCollection  :  " + fruitCollection[2] + "<br />");            document.write("Last Value In FruitCollection  :  " + fruitCollection[3] + "<br />");            //Calculating length of fruitArray collecting by            //calling length method of array object.            document.write("Length of the FruitCollection array is   :  " + fruitCollection.length + "<br />");            //Retriving value of fruitCollection array by using loop.            document.write("Retriving value of fruitArray collection by using loop.<br />");            for (count = 0; count < fruitCollection.length; count++) {                document.write(fruitCollection[count] + "<br />");            }        }     </script></head><body>     <input type="button" value="Click this to view message" onclick="arrayDemo()" /></body></html>
```

##### Output of the following code snippet is as follows

![Array in java script](https://www.mindstick.com/mindstickarticle/fb4fe8cd-89a5-45c1-adcb-38f0a89aad7e/images/a815a6f9-a410-4c50-bfcc-557abbe72831.png)

![Array in java script](https://www.mindstick.com/mindstickarticle/fb4fe8cd-89a5-45c1-adcb-38f0a89aad7e/images/85fcd2be-f711-4109-8f32-b899a0311b4f.png)

\

---

Original Source: https://www.mindstick.com/articles/506/array-in-java-script

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
