what is an array in javascript code example

Example 1: list javascript

//Creating a list
var fruits = ["Apple", "Banana", "Cherry"];

//Creating an empty list
var books = [];

//Getting element of list
//Lists are ordered using indexes
//The first element in an list has an index of 0, 
//the second an index of 1,
//the third an index of 2,
//and so on.
console.log(fruits[0]) //This prints the first element 
//in the list fruits, which in this case is "Apple"

//Adding to lists
fruits.push("Orange") //This will add orange to the end of the list "fruits"
fruits[1]="Blueberry" //The second element will be changed to Blueberry

//Removing from lists
fruits.splice(0, 1) //This will remove the first element,
//in this case Apple, from the list
fruits.splice(0, 2) //Will remove first and second element
//Splice goes from index of first argument to index of second argument (excluding second argument)

Example 2: What is Array?

An array is a container object that holds a fixed number 
of values of a single type. The length of an array is established 
when the array is created. After creation, its length is fixed. 
You have seen an example of arrays already, in the main 
method of the "Hello World!" application.
This section discusses arrays in greater detail.Each item in an array is called an element, 
and each element is accessed by its numerical index.Advantage of Java Array 
o Code Optimization: It makes the code optimized, 
we can retrieve or sort the data easily. 
o Random access: We can get any data located at any index position.Disadvantage of Java Array 
o Size Limit: We can store only fixed size of elements in the array. 
It doesn't grow its size at runtime. To solve this 
problem, collection framework is used in java.

Example 3: array of objects javascript

var widgetTemplats = [
    {
        name: 'compass',
        LocX: 35,
        LocY: 312
    },
    {
        name: 'another',
        LocX: 52,
        LocY: 32
    }
]

Example 4: array in js

function Array() { return []; }

alert(Array(1, 2, 3)); // An empty alert box

Tags:

Java Example