JavaScript Array Methods Part 3

javascript

What is an Array?

An array is an object that can store a collection of objects. Arrays become really useful when you need to store large amounts of the same type of data. Suppose you want to store the details of 500 employees. If you are using variables, you have to create 500 variables, while you can do the same with a single array. You can see an item in terms of its index and the index of the first element of an array is zero. || JavaScript Array Methods

JavaScript Create Array

You can create an array in JavaScript as given below.

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[3] = “Emma”; students[4] = “Rose”; var students = new Array(“John”, “Ann”, “Kevin”);

OR

var students = new Array(); students[0] = “John”; students[1] = “Ann”; students[2] = “Kevin”;

JavaScript Array Methods

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 ().

  1. length property –> If you want to know the number of elements in an array, you can use the length property.
  2. prototype property –> If you want to add new properties and methods, you can use the prototype property.
  3. reverse method –> You can reverse the order of items in an array using a reverse method.
  4. sort method –> You can sort the items in an array using sort method.
  5. pop method –> You can remove the last item of an array using a pop method.
  6. shift method –> You can remove the first item of an array using shift method.
  7. push method –> You can add a value as the last item of the array.

 

<html>

<head>

<title>Arrays!!!</title>

<script type=“text/javascript”>

var students = new Array(“John”, “Ann”, “Aaron”, “Edwin”, “Elizabeth”);

Array.prototype.displayItems=function(){

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

document.write(this[i] + “<br />”);}}

document.write(“students array<br />”);

students.displayItems();

document.write(“<br />The number of items in students array is “ + students.length + “<br />”);

document.write(“<br />The SORTED students array<br />”);

students.sort();

students.displayItems();

document.write(“<br />The REVERSED students array<br />”);

students.reverse();

students.displayItems();

document.write(“<br />THE students array after REMOVING the LAST item<br />”);

students.pop();

students.displayItems();

document.write(“<br />THE students array after PUSH<br />”);

students.push(“New Stuff”);

students.displayItems();

</script>

</head>

<body>

</body>

</html>

 

                                                                                                                                                                                 

2 thoughts on “JavaScript Array Methods Part 3

  1. A large percentage of of the things you say is supprisingly accurate and it makes me ponder the reason why I had not looked at this in this light before. This article really did switch the light on for me as far as this particular topic goes. Nevertheless at this time there is one particular factor I am not really too comfortable with and whilst I attempt to reconcile that with the actual central theme of your position, permit me observe what the rest of your visitors have to point out.Well done.

Comments are closed.