What is an Array?
var students = ["John", "Ann", "Kevin"];
Here, you are initializing your array as and when it is created with values “John”, “Ann” and “Kevin”.The index of “John”, “Ann” and “Kevin” is 0, 1 and 2 respectively. If you want to add more elements to the students array, you can do it like this:
students = "Emma"; students = "Rose"; var students = new Array("John", "Ann", "Kevin");
var students = new Array(); students = "John"; students = "Ann"; students = "Kevin";
Array objects have many properties and methods that help developers handle arrays easily and efficiently. You can get the value of a property by outputting the method by specifying arrayname.property and specifying arrayname.method ().
- length property –> If you want to know the number of elements in an array, you can use the length property.
- prototype property –> If you want to add new properties and methods, you can use the prototype property.
- reverse method –> You can reverse the order of items in an array using a reverse method.
- sort method –> You can sort the items in an array using sort method.
- pop method –> You can remove the last item of an array using a pop method.
- shift method –> You can remove the first item of an array using shift method.
- push method –> You can add a value as the last item of the array.