×

Html & Html5

it training classes Logo

The World's best Software Development Study Portal

Js Array


It is the type of an object in which we can store similar type of elements as well as different type of elements. We have some ways to create the arrays which are described as follows:-:-

  • By arrray literal
  • creating instance of array by using new keyword
  • By using array constructor

Javascript array literal

Syntax

var nameofArray = [value1,value2,value3....valueN] ;

Let's take an example:-

<html>

<body>

<script>

   var fruits = ['Apple','Banana','Litchi'];

   document.write(fruits);

}

</script>

</body>

</html>

Output of this program

Apple,Banana,Litchi

This is one way how we create an array.



Directly by using the new keyword

We use the new keyword to create an array here

Syntax

var nameofArray = new Array();

Let's take an example:-

<html>

<body>

<script>

   var fruits =new Array();

   fruits[0]="Apple";

   fruits[1]="Banana";

   fruits[2]="Litchi";

   document.write(fruits);

}

</script>

</body>

</html>

Output of this program

Apple,Banana,Litchi

This is other way how we create an array by using the new keyword.



Using array constructor

In this way first we create any function with passing the constructor argument with the new keyword.

Let's take an example:-

<html>

<body>

<script>

   var fruits = new Array("Apple","Banana","Litchi");

   document.write(fruits);

}

</script>

</body>

</html>

Output of this program

Apple,Banana,Litchi

This is third way how we create the array.



Some more methods and description below :-

Methods Description
concat() It returns a new array object that contains two or more merged arrays.
every() It determines whether all the elements of an array are satisfying the provided function conditions.
fill() It fills elements into an array with static values.
indexOf() It searches the specified element in the given array and returns the index of the first match.
lastIndexOf() It searches the specified element in the given array and returns the index of the last match.
map() It calls the specified function for every array element and returns the new array
push() It adds one or more elements to the end of an array.
shift() It removes and returns the first element of an array.
slice() It returns a new array containing the copy of the part of the given array.
copywithin() It copies the part of the given array with its own elements and returns the modified array.






JS training insitute | Best IT Training classes in Gurgaon | Web Designing Training Institute