JavaScript Arrays
An array, is a data structure consisting of a collection of elements, each identified by at least one array index or key. It is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. There are 3 ways to construct array in JavaScript- The array literal, which uses square brackets.
- By creating instance of Array directly (using new keyword).
- The array constructor, which uses the new keyword.
Using Array Literal
Syntax
var arrayName=[element1,element1.....elementN];
example
var days=["Sunday","Monday","Tuesday"];
By creating instance of Array directly
var days = new Array();
days[0] = "Sunday";
days[1] = "Monday";
days[2] = "Tuesday";
Using Array Constructor
var days=new Array("Sunday","Monday","Tuesday");
Array Element Type
Array elements can be any type, including number, string, Boolean , null, undefined, object, function, regular expression and other arrays.
var anArray = [500, "car", ["ID", 121], true];
Access the Elements of an Array
In order to access the elements inside an array, you have to use square brackets and array index . JavaScript arrays begin at 0, so the first element will always be inside [0]. example
var days=new Array("Sunday","Monday","Tuesday");
var day_2 = days[1];
alert(day_2);
output
Monday
The length Property
Arrays in JavaScript are zero-based . This means that JavaScript starts counting from zero when it indexes an array. If a JavaScript array has five elements, then that array's "length" property will have a value of 5 (five). But , if a JavaScript array has five elements, the last element has an index of 4. This is because, JavaScript arrays are zero-based. The length property of an object which is an instance of type Array sets or returns the number of elements in that array.
var days=new Array("Sunday","Monday","Tuesday","wednesday","Thursday");
alert(days.length);
When you run the above program, it will return 5.
Looping Array Elements
you can use "for" for Iterating over JavaScript arrays
example
var days=new Array("Sunday","Monday","Tuesday","wednesday","Thursday");
daysLen = days.length;
for (i = 0; i < daysLen; i++) {
alert(days[i]);
}
output
Sunday
Monday
Tuesday
ednesday
Thursday
Another way you can use for loop...
var days=new Array("Sunday","Monday","Tuesday","wednesday","Thursday");
for (let day of days) {
alert( day );
}
output
Sunday
Monday
Tuesday
Wednesday
Thursday
Add to the end of an Array
The push() method will add an element to the end of an array, while its twin function, the pop() method, will remove an element from the end of the array.push() method
var days=new Array("Sunday","Monday","Tuesday");
days.push("wednesday");
for (let day of days) {
alert( day );
}
output
Sunday
Monday
Tuesday
Wednesday
Remove from the end of an Array
pop() method
var days=new Array("Sunday","Monday","Tuesday","wednesday","Thursday");
days.pop();
days.pop();
for (let day of days) {
alert( day );
}
output
Sunday
Monday
Tuesday
Add item(s) to the front of an Array
Using JavaScript Array unshift() Method to add the item in front of an Array.
var days=new Array("Monday","Tuesday","wednesday");
days.unshift("Sunday");
for (let day of days) {
alert( day );
}
output
Sunday
Monday
Tuesday
Wednesday
Remove item(s) from the front of an Array
var days=new Array("Sunday","Monday","Tuesday","wednesday");
days.shift();
for (let day of days) {
alert( day );
}
output
Monday
Tuesday
Wednesday
Modifying Items in Arrays
Array elements are accessed using their index number . Using this index number you can modify an item in an array.
var days=new Array("Sunday","Tuesday","wednesday");
days[0] = "Monday" ;
for (let day of days) {
alert( day );
}
output
Monday
Tuesday
Wednesday
Find the index of an item in the Array
JavaScript indexOf() method returns the index of the first occurrence of a value in an array.
var days=new Array("Sunday","Monday","Tuesday","wednesday");
var idx = days.indexOf("Tuesday");
alert(idx);
The above program return 2.
How do I check if an array includes an object in JavaScript?
You can use the following code for find if an array contains a specific string in JavaScript.
var days = ["Sunday", "Monday", "Tuesday"];
var dayIn = (days.indexOf("Friday") > -1);
alert(dayIn);
The above code retuen "false", because "Friday" is not contains the array "days".
Array.sort
Javascript Array.sort() method sort the elements in an Array either Alphabetic or Numeric, also you can sort ascending or descending. The default sort order is alphabetic and ascending .
var days=new Array("Sunday","Monday","Tuesday","wednesday");
days.sort();
alert(days);
days.reverse();
alert(days)
output
"Sunday","Monday","Tuesday","wednesday"
"wednesday","Tuesday","Monday","Sunday"
Arrays Undefined Values
Arrays are automatically growing and dynamically sized to accommodate any type of data that is added to them. So, when you add or remove elements from an array, the length of the array will change as needed. When you declare an Array with constructor , each slot will be set to "undefined".
var days = new Array(4);
days[0] = "Sunday";
days[1] = "Monday";
days[3] = "Wednesday";
alert(days.length); // will alert 4
alert(days[2]); // will alert "undefined"
output
4
undefined
Multidimensional arrays
A JavaScript multidimensional array is an array of arrays, or, in other words, an array whose elements consist of arrays.
var multiArr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
alert( multiArr[1][1] );
Above program return 5.
JavaScript toString()
The toString() method returns a string representation of an object.
var val =100;
alert(val.toString());
Related Topics