Javascript Theory
Array
An array is an object that can be store a collection of items. An array is a collection of elements. And it is used to store the multiple values in one variable. Each value stored in an array is called an element of the array. An array can be of any type, for example int, char, float ..etc. The first element of an array starts with index zero.
Types of Arrays
An array can be declared in two types:- Array literal
- Array constructor
Array literal
Array literal
It takes a list of values separated by a comma and enclosed in square brackets.
Syntax |
---|
var array-name =[element 0, element1, element 2, ......element N]; Example: var skillpundit = ["C", "Java", "PHP","Python"]; var skillpundit = [1, 2, 3, 4]; |
Array constructor
Array constructor
In array constructor you can use the 'new' keyword to initialize an array.
Syntax |
---|
var array_name = new Array(); var array_name = new Array(element1, element2,...... elementN); Example: var skillpunit = new Array(1, 2, 3, 4); |