Saturday, November 2, 2024
Google search engine
HomeLanguagesJavascriptDifference between array.size() and array.length in JavaScript

Difference between array.size() and array.length in JavaScript

The array.size() method is functionally equivalent to the array.length property and it can be only used in jQuery. Here in JavaScript array.size() is invalid method so array.length property should be used. Below examples implement the above concept: 

Example 1: This example demonstrates array.size() method and array.length property. 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        Difference between array.size() method
        and array.length property
    </title>
</head>
 
<body>
     
<p>
        Click the button to display
        the length of array.
    </p>
 
 
    <button onclick="findLength()">
        Try it
    </button>
 
     
<p>
        Length of the array is:
        <span id="demo"></span>
    </p>
 
 
    <script>
        var arr = ['neveropen', 'for', 'neveropen'];
 
        function findLength() {
            document.getElementById("demo").innerHTML
                        = arr.length;
             
            document.getElementById("demo").innerHTML
                        = arr.size();
        }
    </script>
</body>
 
</html>


Output:

Length of the array is: 3
error on console: TypeError: arr.size is not a function

Note: The array.length property returns value of last_key+1 for Arrays with numeric index value. This property doesn’t guarantee to find the number of items in the array. 

Example 2: This example display how Array.length property works. 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        Array.length property
    </title>
</head>
 
<body>
     
<p>
        Click the button to display
        the length of array.
    </p>
 
 
    <button onclick="findLength()">
        Try it
    </button>
 
     
<p>
        Length of array the is:
        <span id="demo"></span>
    </p>
 
 
    <script>
        var arr = ['neveropen', 'for', 'neveropen'];
        arr[50] = 'article';
         
        function findLength() {
            document.getElementById("demo")
                        .innerHTML = arr.length;
        }
    </script>
</body>
 
</html>


Output:

Length of the array is: 51

Example 3: This example display how array.length property works when index key is non numeric. 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        array.length property with
        non-numeric index key
    </title>
</head>
 
<body>
     
<p>
        Click the button to display
        the length of array.
    </p>
 
 
    <button onclick="findLength()">
        Try it
    </button>
 
     
<p>
        Length of array the is:
        <span id="demo"></span>
    </p>
 
 
    <script>
        var arr = new Array();
        arr['a'] = 1;
        arr['b'] = 2;
        arr['c'] = 3;
         
        function findLength() {
            document.getElementById("demo")
                    .innerHTML = arr.length;
        }
    </script>
</body>
 
</html>


Output:

Length of the array is: 0

Let us see the differences in a tabular form -:

  array.size() array.length()
1. This property returns the size of the array This property returns the size of the array 
2.

Its syntax is -:

array.size()

Its syntax is -:

array.length

3. It cannot be used in Javascript but only used in Jquery It is used in Javascript, not in jQuery
4. This is a feature of jQuery array.length is an ECMAScript1 feature.
5

Its supported browsers are -:

Chrome , Internet Explorer, Firefox, Safari, Opera Microsoft Edge

Its supported browsers are -:

Chrome , Internet Explorer, Firefox, Safari, Opera, Microsoft Edge

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments