Home
Aptitude
Categories


Home » JavaScript

JavaScript | Common properties and methods of Array



Here, we are going to learn about array’s most useful common properties and methods in the JavaScript with examples.
Submitted by Pankaj Singh, on October 10, 2018

Common properties and methods of array in JavaScript

Properties/Methods Descriptions
array.length Returns the length of the array/total number of elements of the array
array[index] Returns the item name stored at “index” position
array.push(item) Inserts item at the end of the array
array.unshift(item) Inserts item at the front of the array
array.pop() Removes item from the end of the array
array.shift() Removes item from the end of the array
array.indexOf(item) Returns the index of the item
array.splice(index,1)) Removes item from the list (index must be provided)

Code:

<html>
    <head>
        <script>
            var fruits = [
                "apple",
                "mango",
                "banana",
                "grapes",
                "guava"
            ];
        </script>
    </head>
    <body>
        <script>
            document.write("<h3>List Of Array Items</h3>")
            document.write("<hr />");
            document.write("<ul>");
            for(var i=0;i<fruits.length;i++){
                document.write("<li>"+fruits[i]+"</li>");
            }
            document.write("</ul>");
            document.write("<hr />")
            
            var size=fruits.length;
            document.write("Size of Array : "+size+"<br />");
            
            var first = fruits[0];
            document.write("First Item of Array : "+first+"<br /><br />");
            
            fruits.forEach(function(item,index,array){
                document.write("Fruits["+index+"] = "+item+"<br />");
            });
            
            
            //Insert Item at last
            fruits.push("orange");
            document.write("<br />")
            fruits.forEach(function(item,index,array){
                document.write("Fruits["+index+"] = "+item+"<br />");
            });
            
             //Insert Item at Front
            fruits.unshift("cherry");
            document.write("<br />")
            fruits.forEach(function(item,index,array){
                document.write("Fruits["+index+"] = "+item+"<br />");
            });
            
            //Remove Item from Last
            fruits.pop();
            document.write("<br />")
            fruits.forEach(function(item,index,array){
                document.write("Fruits["+index+"] = "+item+"<br />");
            });
            
            //Remove Item from Front
            fruits.shift();
            document.write("<br />")
            fruits.forEach(function(item,index,array){
                document.write("Fruits["+index+"] = "+item+"<br />");
            });
            
            
            //Finding Index of Item
            var index = fruits.indexOf("banana");
            document.write("<br />");
            document.write("Index of banana : "+index+"<br />");
            
            
            //Removing any item from List
            document.write("<br />");
            var pos = fruits.indexOf("banana");
            fruits.splice(pos,1)
            fruits.forEach(function(item,index,array){
                document.write("Fruits["+index+"] = "+item+"<br />");
            });
        </script>
    </body>
</html>

Output

List Of Array Items
apple
mango
banana
grapes
guava
Size of Array : 5
First Item of Array : apple

Fruits[0] = apple
Fruits[1] = mango
Fruits[2] = banana
Fruits[3] = grapes
Fruits[4] = guava

Fruits[0] = apple
Fruits[1] = mango
Fruits[2] = banana
Fruits[3] = grapes
Fruits[4] = guava
Fruits[5] = orange

Fruits[0] = cherry
Fruits[1] = apple
Fruits[2] = mango
Fruits[3] = banana
Fruits[4] = grapes
Fruits[5] = guava
Fruits[6] = orange

Fruits[0] = cherry
Fruits[1] = apple
Fruits[2] = mango
Fruits[3] = banana
Fruits[4] = grapes
Fruits[5] = guava

Fruits[0] = apple
Fruits[1] = mango
Fruits[2] = banana
Fruits[3] = grapes
Fruits[4] = guava

Index of banana : 2

Fruits[0] = apple
Fruits[1] = mango
Fruits[2] = grapes
Fruits[3] = guava





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.