over 9 years ago
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>
<!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 :
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>
<!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 :
Can you help out the community by solving one of the following Javascript problems?
Do activity (Answer, Blog) > Earn Rep Points > Improve Rank > Get more opportunities to work and get paid!
For more topics, questions and answers, please visit the Tech Q&A page.
0 Comment(s)