Array Object in JavaScript

Introduction

 
As per our Bookies knowledge, an "Array object is used to store multiple values in a single variable." Here an array can hold various types of data types in a single array slot, which implies that an array can have a string, a number or an object in a single slot. This implies that an array can have a string, a number, or an object in a single slot. 
 
ArrayObject1.jpg
 
Using the Array Constructor
 
An empty array in cases where we do know the exact number of elements to be inserted in the Array. We can create an empty by using an array constructor.
 
Syntax
 
ArrayObject2.jpg
 
If we want to assign the size of the array then:
 
ArrayObject3.jpg
 
For example, if we have 20 items then the syntax will be: "var myarray=new array(20)". Here 20 means 20 items in the array.
 
Similar to the given syntax, if elements or items are given then we can write it like: 
 
ArrayObject4.jpg
 
Using the Array Literal Notation: As we know an array can be created using the array literal notation. The array literal notation is a comma-separated list of items enclosed by square brackets.
 
Syntax
 
ArrayObject5.jpg
 
Similar to the constructor method, if we have elements or size of the Array then put inside the brackets: 
 
ArrayObject6.jpg
 
So there is the syntax of an Array object. In the first method, the constructor holds the value of the Constructor function that created the object. The length holds the number of elements in an array while the prototype adds properties and methods to the Array objects. 
 
Concat(), join(), pop(), push(), reverse(), shift(), slice(), sort(), splice(), tostring(), unshift(), valueof() these are the some Array object methods.
 
Now let's do an exercise.
 
Write some simple code as in the following:
 
ArrayObject7.jpg
 
In the given code, two array objects with the names myarray1 and myarray2 are created using the new keyword. The object has been assigned the array elements passed to it as parameters.
 
The two array objects are concatenated using the concat()method.
 
The elements of the myarray1 object have been joined into a string using the join() method.
 
The last element of the myarray1 object is extracted and printed using the pop() method.
 
The element of the myarray1 object is reversed by the reverse() method. The first element of the myarray1 object, which comes out to be Yahoo, is extracted and printed using the shift() method, Now myarray1 has only three elements left, that are alphabetically sorted using the sort() method. Finally, the myarray2 object is converted using the toString() method.
 
Let's see the result of the code above:
 
ArrayObject8.jpg
 
So this is a little bit about objects, do more exercises and submit your queries here.
 
Keep reading.


Similar Articles
Foreantech
Foreantech - A complete online solution company.