Introduction
In general, we know that the term "item" refers to "items" - item refers to a single object while the term items refers to the group or collection of items.
If we take an example of a school as our case study, the head of the school, "Principal", can be taken in a variable.
- Var principal = "abcdname";
In the same case, if we think about students, there will be "n" number of students. So, we can't refer to all students within a general variable.
As the students can be treated as a collection, we can think of "arrays".
- var students =["st1","st2","st3"]
Hope you understood the scenario of taking the arrays.
Declaration of arrays
-> Empty array,
The above line states the declaraion of an empty array.
->Array with elements.
- var students = ["stu1","stu2","stu3"]
The above line states that the array has been declared with 3 items.
Accessing the array elements
In general, arrays use the concept of indexing for specifying or identifying the particular element from the group of elements, and of course, always indexing starts from zero(0).
- var students = ["stu1","stu2","stu3"];
-> Accessing 1st element - alert(students[0]); --> Results "stu1"
-> Accessing 2nd element - alert(students[1]); --> Results "stu2"
-> Accessing 3rd element - alert(students[2]); --> Results "stu3"
And more importantly, if I try to access the out of array count value which here is students[3] where index 3 is not there from the above code, we will get the result as "undefined".
-> Accessing non existing index - alert(students[3]); --> Results "undefined"
Now if I try to access the whole students object, we'll be getting the whole array elements as a string with each element separated by comma(,).
-->Accessing the array - alert(students); --> Results "stu1,stu2,stu3"
Getting length of array
We are having the property "length" to find out the length of the array alert (students.length) --> Produces 3 as the result on alert.
Adding an element to the existing array
The javascript provided us some default functions for array object as follows.
push()
This function is used to add the elements into the array.
Ex
students.push("stu4"); --> Adds another element to the students array.
By the above line a new element "stu4"is going to get added at the end of the array.
Now if I say alert(students), the values on alert box would be as "stu1,stu2,stu3,stu4".
unshift()
This function is used to add the elements at the top of the array unlike the push() at the end of the array.
Example
- students.unshift("stu5");
By the above line a new element "stu5" is going to get added at the 0th index of the array.
Now if I say alert (students), the values on alert box would be as "stu5,stu1,stu2,stu3,stu4".
Removing an element from the array
splice()
Splice is the array object function where we can remove a specific element by it.
Ex
- var students = ["stu1","stu2","stu3"];
- students.splice(1,1);
From the above line the splice function says removing 1 (1st argument) element from the index of 1 (2nd element). removes the "stu2" from the elements.