JavaScript Arrays : Array is a like a container which contains the same type of objects or elements. Arrays has fixed length. We can access the array elements using the index value.The index of the array starts from 0.
Example of creating array in JavaScript : Here in following example I will show you how to create a array in JavaScript.
CreateArray.html
<!DOCTYPE html>
<html>
<body>
<p>To create an array and display it's constructor, Click the button.</p>
<button onclick="createArray()">Create Array</button>
<p id="container"></p>
<script>
function createArray() {
var students = ["Ashok", "Nitin Kumar", "Rajesh", "Sumit"];
document.getElementById("container").innerHTML = students;
}
</script>
</body>
</html>
Output :
Ashok,Nitin Kumar,Rajesh,Sumit
Array has a property called constructor, this is used to know the constructor method of the object.
Example of the Array constructor property :
<!DOCTYPE html>
<html>
<body>
<p>To create an array and display it's constructor, Click the button.</p>
<button onclick="createArray()">Create Array</button>
<p id="container"></p>
<script>
function createArray() {
var students = ["Ashok", "Nitin Kumar", "Rajesh", "Sumit"];
document.getElementById("container").innerHTML = students.constructor;
}
</script>
</body>
</html>
Output :
function Array() { [native code] }
0 Comment(s)