- JavaScript Basics
- JavaScript Home
- JavaScript Syntax
- JavaScript Placements
- JavaScript Output
- JavaScript Statements
- JavaScript Keywords
- JavaScript Comments
- JavaScript Variables
- JavaScript var
- JavaScript let
- JavaScript const
- JavaScript var Vs let Vs const
- JavaScript Operators
- JavaScript Comparison/Logical
- JavaScript Data Types
- JS Conditional Statements
- JavaScript Conditional Statement
- JavaScript if Statement
- JavaScript if-else Statement
- JavaScript switch Statement
- JavaScript Loops
- JavaScript for Loop
- JavaScript while Loop
- JavaScript do-while Loop
- JavaScript Break Continue
- JavaScript Popup Boxes
- JavaScript Dialog Box
- JavaScript alert Box
- JavaScript confirm Box
- JavaScript prompt Box
- JavaScript Functions
- JavaScript Functions
- JS Function with Parameter
- JavaScript Return Statement
- JavaScript Variable Scope
- JavaScript setTimeout() Method
- JavaScript setInterval() Method
- JavaScript Events
- JavaScript Events
- JavaScript onclick Event
- JavaScript onload Event
- JavaScript Mouse Events
- JavaScript onreset Event
- JavaScript onsubmit Event
- JavaScript Objects
- JavaScript Objects
- JavaScript Number Object
- JavaScript Array Object
- JavaScript String Object
- JavaScript Boolean Object
- JavaScript Math Object
- JavaScript RegExp Object
- JavaScript Date Object
- JavaScript Browser Objects
- JavaScript Browser Objects
- JavaScript Window Object
- JavaScript Navigator Object
- JavaScript History Object
- JavaScript Screen Object
- JavaScript Location Object
- JavaScript Document Object
- JS Document Object Collection
- JS Document Object Properties
- JS Document Object Methods
- JS Document Object with Forms
- JavaScript DOM
- JavaScript DOM
- JavaScript DOM Nodes
- JavaScript DOM Levels
- JavaScript DOM Interfaces
- JavaScript Cookies
- JavaScript Cookies
- JavaScript Create/Delete Cookies
- JavaScript Advance
- JavaScript Regular Expression
- JavaScript Page Redirection
- JavaScript Form Validation
- JavaScript Validations
- JavaScript Error Handling
- JavaScript Exception Handling
- JavaScript try-catch throw finally
- JavaScript onerror Event
- JavaScript Multimedia
- JavaScript Animation
- JavaScript Image Map
- JavaScript Debugging
- JavaScript Browser Detection
- JavaScript Security
- JavaScript Misc
- JavaScript innerHTML
- JavaScript getElementById()
- JS getElementsByClassName()
- JS getElementsByName()
- JS getElementsByTagName()
- JavaScript querySelector()
- JavaScript querySelectorAll()
- JavaScript document.write()
- JavaScript console.log()
- JavaScript Programs
- JavaScript Programs
- JavaScript Test
- JavaScript Online Test
- Give Online Test
- All Test List
JavaScript Arrays
JavaScript arrays are basically used in storing multiple values in a single variable.
Create an Array in JavaScript
Here is an example, shows how to create an array in JavaScript:
var fruits = new Array( "Guava", "Apple", "Orange" );
You can also create an array in JavaScript, simply by assigning the values like this:
var fruits = [ "Guava", "Apple", "Orange" ];
The fruits[0] represents the first element which is "Guava" here, and the fruits[2] is the third element which is "Orange" here.
Display Array Element in JavaScript
Here is an example, shows how to display/print array element in JavaScript:
<!DOCTYPE html> <html> <head> <title>JavaScript Array Object Example</title> </head> <body> <p id="array_object_para"></p> <script> var cars = ["Audi", "Mercedes", "BMW"]; document.getElementById("array_object_para").innerHTML = cars[0]; </script> </body> </html>
Here is the output produced by the above Array object example in JavaScript.

Here is the live demo output of the above JavaScript Array Object example program.
Create an Array in JavaScript using new Keyword
Here is an example, shows how to create an array in JavaScript using the keyword new:
<!DOCTYPE html> <html> <head> <title>JavaScript Array Object Example</title> </head> <body> <p id="array_object_para2"></p> <script> var cars = new Array("Audi", "Mercedes", "BMW"); document.getElementById("array_object_para2").innerHTML = cars[0]; </script> </body> </html>
It will display the same output as produced from the above JavaScript Array Object example program.
Access Array Elements in JavaScript
To access any elements present in a array, then index the array. For example, if you want to access the first element of a array named myArr, then index this array as myArr[0]. Here is an example:
var name = cars[0];
You can also alter the value present at particular index number of the array. Here is an example, alter the value of the first element of the array cars:
cars[0] = "Opel";
JavaScript Array Object Properties
The table given below describes properties of the Array object in JavaScript.
Property | Description |
---|---|
constructor | holds the value of the constructor function that has created the object |
length | holds the number of elements in an array |
prototype | adds properties and methods to the Array object |
JavaScript Array Object Methods
The table given here describes methods of the Array object in JavaScript.
Method | Description |
---|---|
concat() | joins two or more arrays |
join() | joins all the elements of an array into a string |
pop() | removes the last element of an array and returns that element |
push() | adds new element as the last element and the returns the length of the new array |
reverse() | reverses the order of list of element in an array |
shift() | removes the first element of an array and then returns that element |
slice() | selects a part of an array and then returns that part as a new array |
sort() | sorts the elements of an array |
splice() | adds or removes the elements of an array |
tostring() | converts an array into a string and then returns the string |
unshift() | adds new elements to an array and then returns the new length |
valueof() | returns the primitive value of an Array object |
JavaScript Array Example
Here is an example demonstrates array in JavaScript.
<!DOCTYPE HTML> <HTML> <HEAD> <TITLE>JavaScript Array Example</TITLE> <SCRIPT type="text/javascript"> js_array_var1=new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"); js_array_var2=new Array(1, 2, 3, 4, 5); document.write("Array 1 : "+js_array_var1+"<HR/>") document.write("Array 2 : "+js_array_var2+"<HR/>") document.write("Concatenated Both Arrays : "+js_array_var1.concat(js_array_var2+"<HR/>")) document.write("Joined Both Arrays : "+js_array_var1.join(js_array_var2)+"<HR/>") document.write("Array 1 Popped Out : "+js_array_var1.pop()+"<HR/>") document.write("Reversed Array 1: "+js_array_var1.reverse()+"<HR/>") document.write("Shifted Array 1 : "+js_array_var1.shift()+"<HR/>") document.write("Sorted Array 1 : "+js_array_var1.sort()) </SCRIPT> </HEAD> <BODY> </BODY> </HTML>
Here is the output produced by the above JavaScript array example.

Here is the live demo output of the above Array object example in JavaScript.
« Previous Tutorial Next Tutorial »