• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

JavaScript Array Object

April 10, 2014 //  by Krishna Srinivasan//  Leave a Comment

Array is an group of element which is stored in a single object variable. If you have list of values of same type, then all the values can be stored in a array object with the index pointing to each value. All the elements in the array would be in the same type. The values stored in the array can be retrieved using the loop or any other mechanism.

JavaScript Array Object Syntax

Following is the syntax to define array variables in JavaScript.

var list = new array[2, 5 ,7,9]

Array Properties

Property Description
constructor It is used to provide a reference to array function of the particular object.
prototype It is used to add properties and methods
length It is used to return the number of elements present in array

Example of Array Property

<!DOCTYPE html>
<body>
<script type="text/javascript">
        var a = new Array( 4, 67, 345, 20, 9 );
	document.write("Array:" +a);
   	document.write("<br>The length of array is : " + a.length);
	document.write("<br>The constructor of array is:" + a.constructor);
</script>
</body>
</html>
  • In the above program we have used array properties.
  • We have used two properties in the program i.e. length and constructor.
  • We have set an array variables “var a = new Array( 4, 67, 345, 20, 9 );” where we have declared 5 values in it.
  • document.write(“The length of array is : ” + a.length); is used to specify the length of the array.
  • document.write(“The constructor of array is:” + a.constructor); is used to provide a reference to array function.

JavaScript Array Property Demo

  • Save the file as array_property.html in your system.
  • Just open the file in the browser, you will see the below picture in the browser.

When the execution process is completed successfully we will get the following output:

JavaScript Array Property Demo

Array Methods

Property Description
forEach( ) It is used to call the array of element for each time.
every( ) It is used when every array element satisfies the function to be true.
filter( ) It is used to filter all existing array an create a new array.
concat( ) It is used to joins the two string of array and represent it as one string array.
join( ) It is used to convert all of the elements of an array to strings.
push( ) It is used to add an element to the high end of an array.
pop( ) It is used to remove an element from the high end of an array.
shift( ) It is used to remove an element to the beginning of an array.
unshift( ) It is used to add an element to the beginning of an array.
indexOf( ) It is used to gives the first occurrence of an element in the array.
lastIndexOf( ) It is used to gives the last occurrence of an element in the array.
map( ) It is used to map all function of array into a new array.
reverse( ) It is used to reverse the order of the elements of the array object which it is called.
slice( ) It is used to return a part of the array object as a new array.
some( ) It is used when atleast one array element satisfies the function to be true.
sort( ) It is used to sort the elements of the array to strings.
splice( ) It is used to remove or insert new elements in the array.
reduce( ) It is used to reduce the array value from left to right in a single value.
reduceRight( ) It is used to reduce the array value from right to left in a single value.
toSource( ) It is used to return the array using source code of the object
toString( ) The elements of the object is converted to a string.

Example of Array Methods

<!DOCTYPE html>
<head>
</head>
<body>
<script type="text/javascript">
	var number = [6, 8, 2];
	document.write("Original Array: "+number);
	var sorted = number.sort();
	document.write("<br>The sorted array : " + sorted );

	var element = number.pop();
	document.write("<br>The element deleted is: " + element );

	var length = number.push(10);
	document.write("<br>The new array is : " + number );
</script>
</body>
</html>
  • In the above program we have used array methods.
  • We have used few methods in this program i.e. pop, push, and sort.
  • We have set an array variables “var number = [6, 8, 2];” where we have declared 3 values in it.
  • var sorted = number.sort(); is used to sort the number of array given in variable .
  • var element = number.pop(); is used to remove number from back end of the array list.
  • var length = number.push(10); is used to add element from back end of the array list.
  • “document.write” is used to display the element which are sorted, added in the array list.

JavaScript Array Methods Demo

  • Save the file as array_methods.html in your system.
  • Just open the file in the browser, you will see the below picture in the browser. Note that the browser must support HTML specification.

When the execution process is completed successfully we will get the following output:

JavaScript Array Methods Demo

Category: JavaScriptTag: JavaScript Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « JSF Custom Error Pages
Next Post: Alert Messages in Javascript »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact