JavaScript Arrays


JavaScript arrays are used to store multiple values in a single variable.

There are 3 ways to construct array in JavaScript

  • By array literal
  • By creating instance of Array directly (using new keyword)
  • By using an Array constructor (using new keyword)

What is an Array?

An array is a special variable, which can hold more than one value at a time.


JavaScript array literal

Using an array literal is the easiest way to create a JavaScript Array.

Syntax

var array_name=[value1,value2.....valueN];  

Example

<script type="text/javascript">  
var student=["Ashwani","Sachin","Deepak","Soun"];  
for (i=0; i<student.length; i++)
 {  
  document.write(student[i] + "<br/>");  
 }  
</script>  
Output
Ashwani
Sachin
Deepak
Soun

JavaScript Array directly (new keyword)

new keyword is used to create instance of array.

Syntax

var array_name= new Array();

Example

<script type="text/javascript">
var i;  
var student = new Array();  
student[0] = "Ashwani";  
student[1] = "Sachin";  
student[2] = "Soun";
student[3] = "Deepak";  
  
for (i=0; i<student.length; i++) 
 {  
   document.write(student[i] + "<br>");  
 }  
</script>  
Output
Ashwani
Sachin
Soun
Deepak

JavaScript array constructor (new keyword)

To create instance of array by passing arguments in constructor so that we don't have to provide value explicitely

Example

<script type="text/javascript">
var i;  
var student = new Array("Ashwani","Sachin","Soun","Deepak")
  
for (i=0; i<student.length; i++) 
 {  
   document.write(student[i] + "<br>");  
 }  
</script>  
Output
Ashwani
Sachin
Soun
Deepak

Difference Between Arrays and Objects?

In JavaScript, arrays use numbered indexes.

In JavaScript, objects use named indexes.


When to Use Arrays? When to use Objects?

  • JavaScript does not support associative arrays.
  • You should use objects when you want the element names to be strings (text).
  • You should use arrays when you want the element names to be numbers.

Array Methods

MethodDescription
concat()Returns a new array comprised of this array joined with other array(s) and/or value(s).
every()Returns true if every element in this array satisfies the provided testing function.
filter()Creates a new array with all of the elements of this array for which the provided filtering function returns true.
forEach()Calls a function for each element in the array.
indexOf()Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
join()Joins all elements of an array into a string.
lastIndexOf()Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.
map()Creates a new array with the results of calling a provided function on every element in this array.
pop()Removes the last element from an array and returns that element.
push()Adds one or more elements to the end of an array and returns the new length of the array.
reduce()Apply a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.
reduceRight()Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.
reverse()Reverses the order of the elements of an array -- the first becomes the last, and the last becomes the first.
shift()Removes the first element from an array and returns that element.
slice()Extracts a section of an array and returns a new array.
some()Returns true if at least one element in this array satisfies the provided testing function.
toSource()Represents the source code of an object
sort()Sorts the elements of an array.
splice()Adds and/or removes elements from an array.
toString()Returns a string representing the array and its elements.
unshift()Adds one or more elements to the front of an array and returns the new length of the array.

Share this article on