JavaScript arrays are used to store multiple values in a single variable. They are like sets in mathematics. It stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data.
You can create an array as
JSvar myarray = new Array(element1,element2,element3);
//or
var myarray2 = [element1, element2, element3];
The maximum length allowed for an array is 4,294,967,295.
Spaces and line breaks are not important. A declaration can span multiple lines
JSvar socialMedia = [
"Facebook",
"Twitter",
"Instagram",
"Snapchat",
"Hike"
];
You can create an array as
var myarray = new Array(element1,element2,element3); //or var myarray2 = [element1, element2, element3];
var socialMedia = [ "Facebook", "Twitter", "Instagram", "Snapchat", "Hike" ];
Access the Elements of an Array
You refer to an array element by referring to the index number.You can also use them to edit the value of element, add element or to assign the value to a variable.
This will assign the value of 2nd element of array socialMedia to a new variable.
JSvar medium = socialMedia[1];
This statement will change the value of element
JSsocialMedia[3] = "Medium";
Remember that the counting starts from 0 not 1
Arrays are special type of objects. The typeof
operator returns "object" for the arrays
var medium = socialMedia[1];
socialMedia[3] = "Medium";
typeof
operator returns "object" for the arraysArray element can be anything
You can have objects in an Array. You can have functions in an Array. You can have arrays in an Array:
JSmyArray[0] = Date.now;
myArray[1] = myFunction;
myArray[2] = socialMedia;
myArray[0] = Date.now; myArray[1] = myFunction; myArray[2] = socialMedia;
The length property
The length property returns the length of an array(number of elements present in an array).
JSvar companies = ["Microsoft", "IBM", "Apple", "Google"];
companies.length;
// the length of companies is 4
var companies = ["Microsoft", "IBM", "Apple", "Google"]; companies.length; // the length of companies is 4
Adding Array elements
The easiest way to add an element in an array is using array's push method
JSvar companies = ["Microsoft", "IBM", "Apple", "Google"];
companies.push("Hathaway");
You can also use the index values to add or edit an element
JSvar companies = ["Microsoft", "IBM", "Apple", "Google"];
companies[5] = "Hathaway";
Remember that using this method we also create undefined elements. For example as we had 4 elements in array and we added an element with index 5, hence on 6th place so we created undefined values for 4th and 5th elements
var companies = ["Microsoft", "IBM", "Apple", "Google"]; companies.push("Hathaway");
var companies = ["Microsoft", "IBM", "Apple", "Google"]; companies[5] = "Hathaway";
Recognizing an array
Since the typeof operator returns the value object for an array, we need to use different methods to recognize an array.
JSvar companies = ["Microsoft", "IBM", "Apple", "Google"];
typeof companies;
//returns object
We may use Array.isArray() method of an array.
JSArray.isArray(companies);
//returns true
Or we may use the instanceof method
JScompanies instanceof Array;
//returns true
var companies = ["Microsoft", "IBM", "Apple", "Google"]; typeof companies; //returns object
Array.isArray(companies); //returns true
companies instanceof Array; //returns true
Comments
Post a Comment