- •Introduction
- •1 Working with javascript objects and arrays
- •JavaScript is an Interpreted Language
- •1.2 JavaScript History Object, Working with JavaScript Objects and Arrays (JavaScript)
- •Intrinsic objects, such as Array and String.
- •Arrays as Objects. In JavaScript, objects and arrays are handled almost identically, because arrays are merely a special kind of object. Both objects and arrays can have properties and methods.
- •Create an Array in JavaScript. Here is an example, shows how to create an array in JavaScript:
- •Display Array Element in JavaScript. Here is an example, shows how to display/print array element in JavaScript:
- •2.1 Specify a reference to the result of the report, which appeared in arrays of javascript
- •2 Reports using objects and arrays javascript
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.
Audi
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 |
Example: Sorting an Array. The sort() method sorts an array alphabetically:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array Sort</h2>
<p>The sort() method sorts an array alphabetically.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits.sort();
document.getElementById("demo").innerHTML = fruits;
}
</script>
</body>
</html>
