0% found this document useful (0 votes)
18 views19 pages

Array

Arrays are collections of data items stored under a single name. They provide a mechanism for declaring and accessing multiple data items with a single identifier, simplifying data management. Arrays allow dealing with multiple data items and are a special type of object in JavaScript.

Uploaded by

dk1078451
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views19 pages

Array

Arrays are collections of data items stored under a single name. They provide a mechanism for declaring and accessing multiple data items with a single identifier, simplifying data management. Arrays allow dealing with multiple data items and are a special type of object in JavaScript.

Uploaded by

dk1078451
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Array

Arrays are collection of data items stored under a single name.


Array provide a mechanism for declaring and accessing several
data items with only one identifier, thereby simplifying the task
of data management.
We use array when we have to deal with multiple data items.
Arrays are a special type of objects. The typeof operator in
JavaScript returns "object" for arrays.
Declaration and initialization of Array
• Using Array Literal
Syntax: - var array_name = [ ];
Ex: -
var geek = [ ];
geek[0] = "Rahul";
geek[1] = "Ram";
geek[2] = 56;
geek[42] = “Jay”;
Declaration and initialization of Array
• Using Array Literal
Syntax: - var array_name = [value1, value2, value_n];
Ex: - var geek = [“Rahul”, “Ram”, 56, “Jay”];

var geek = [, , , ,];


All Values are undefined
var geek = [, , , 45, , , 78];

var a = 10, b = 20, c = 30;


var geek = [a, b, c];
var geek = [“Rahul”, “Ram”, 56, “Jay”];

Index Value
geek[0] Rahul
geek[1] Ram
geek[2] 56
geek[3] Jay

Note - By default, array starts with index 0.


Declaration and initialization of Array
• Using Array Constructor
Syntax: - var array_name = new Array( );
Ex: - var geek = new Array ( ); var geek = [ ];
geek[0] = "Rahul";
geek[1] = "Ram";
geek[2] = 56;
geek[42] = “Jay”;
Declaration and initialization of Array
• Using Array Constructor
Syntax: - var array_name = new Array(value1, value2, value_n);
Ex: - var geek = new Array(“Rahul”, “Ram”, 56, “Jay”);
Ex: - var geek = new Array(10, 20, 30, 40, 50);
var geek = [“Rahul”, “Ram”, 56, “Jay”];
Syntax: - var array_name = new Array(single_numeric_value);
Ex: - var geek = new Array(5);
This will create an empty array with 5 length. So this is not good idea to use Array
Constructor if you have only single numeric value.
var geek = new Array(“Rahul”, “Ram”, 56, “Jay”);
var geek = [“Rahul”, “Ram”, 56, “Jay”];

Index Value
geek[0] Rahul
geek[1] Ram
geek[2] 56
geek[3] Jay

Note - By default, array starts with index 0.


Important Points
• JavaScript arrays are zero-indexed: the first element of an array is at index 0.
• Using an invalid index number returns undefined.
• It's possible to quote the JavaScript array indexes as well (e.g., geek['2']
instead of geek[2]), although it's not necessary.
• Arrays cannot use strings as element indexes but must use integers.
• There is no associative array in JavaScript.
geek[“fees”] = 200;
• No advantage to use Array Constructor so better to use Array Literal for
creating Arrays in JavaScript.
Accessing Array Elements
var geek = [“Rahul”, “Ram”, 56, “Jay”];
document.write(geek[0]); Index Value

document.write(geek[1]); geek[0] Rahul


geek[1] Ram
document.write(geek[2]);
geek[2] 56
document.write(geek[3]);
geek[3] Jay
document.write(geek[23]); geek[23]

Undefined
Accessing Array Elements
Access all at once
var geek = [“Rahul”, “Ram”, 56, “Jay”];
document.write (geek);

var geek = [ ]; var geek = [ ];


geek[0] = "Rahul"; geek[0] = "Rahul";
geek[1] = "Ram"; geek[1] = "Ram";
geek[2] = 56;
geek[2] = 56;
geek[20] = “Jay”;
document.write (geek); document.write(geek);
Modifying Array Elements
var geek = [“Rahul”, “Ram”, 56, “Jay”];
document.write(geek);
geek[0] = “Rohit”;
document.write(geek);

var geek = [“Rahul”, “Ram”, 56, “Jay”];


var geekyshows = geek;
document.write(geekyshows);
document.write(geek);
geekyshows[0] = “Rohit”;
document.write(geek);
Removing Array Elements
Array elements can be removed using delete operator. This
operator sets the array element it is invoked on to undefined but
does not change the array’s length.
Syantx :- delete Array_name[index];
Ex:- delete geek[0];
Length Property
The length property retrieves the index of the next available position at the
end of the array. The length property is automatically updated as new elements
are added to the array. For this reason, length is commonly used to iterate
through all elements of an array.
var geek = [“Rahul”, “Ram”, 56, “Jay”];
document.write(geek.length);
for loop with Array
var geek = [“Rahul”, “Ram”, 56, “Jay”];
for (var i = 0; i<= 3; i++){
document.write(geek[i] + “<br>”);
}

for (var i = 0; i< geek.length; i++){


document.write(geek[i] + “<br>”);
}
forEach Loop
The forEach calls a provided function once for each element in an array, in order.
Syntax: - array.forEach(function (value, index, arr) {
});
Where,
value – It is the current value of array index.
index – Array’s index number
arr - The array object the current element belongs to
Ex:- geek.forEach( function(name){
document.write(name);
} );
for of Loop
The for...of statement creates a loop iterating over iterable objects.
Syntax: -
for (var variable_name of array) {
}

Ex: -
for (var value of geek){

}
Input from User in Array
You can get input from user in an empty array :-
• var geek= [ ];
• var geek = new Array( );
• var geek = new Array(3); // 3 is length of array
Multidimensional Array
Multidimensional array is Arrays of Arrays.
Multidimensional array can be 2D, 3D, 4D etc.
Ex: -
2D - var name[ [ ], [ ], [ ] ]
Multidimensional Array
Rahul Dell 10
Sonam Hp 20
Sumit Zed 30

[0][0] Rahul [0][1] Dell [0][2] 10


[1][0] Sonam [1][1] Hp [1][2] 20
[2[0] Sumit [2][1] Zed [2][2] 30

You might also like