Diferencia entre array.size() y array.length en JavaScript

El método array.size() es funcionalmente equivalente a la propiedad array.length y solo se puede usar en jQuery . Aquí, en JavaScript , array.size() es un método no válido, por lo que se debe usar la propiedad array.length . Los siguientes ejemplos implementan el concepto anterior: 

Ejemplo 1: Este ejemplo demuestra el método array.size() y la propiedad array.length. 

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 = ['geeks', 'for', 'geeks'];
 
        function findLength() {
            document.getElementById("demo").innerHTML
                        = arr.length;
             
            document.getElementById("demo").innerHTML
                        = arr.size();
        }
    </script>
</body>
 
</html>

Producción:

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

Nota: La propiedad array.length devuelve el valor de last_key+1 para arrays con valor de índice numérico. Esta propiedad no garantiza encontrar la cantidad de elementos en la array. 

Ejemplo 2: Este ejemplo muestra cómo funciona la propiedad Array.length. 

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 = ['geeks', 'for', 'geeks'];
        arr[50] = 'article';
         
        function findLength() {
            document.getElementById("demo")
                        .innerHTML = arr.length;
        }
    </script>
</body>
 
</html>

Producción:

Length of the array is: 51

Ejemplo 3: este ejemplo muestra cómo funciona la propiedad array.length cuando la clave de índice no es numérica. 

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>

Producción:

Length of the array is: 0

Veamos las diferencias en forma tabular -:

  array.tamaño() array.longitud()
1. Esta propiedad devuelve el tamaño de la array. Esta propiedad devuelve el tamaño de la array. 
2.

Su sintaxis es -:

array.tamaño()

Su sintaxis es -:

array.longitud

3. No se puede usar en Javascript, solo se usa en Jquery Se usa en Javascript, no en jQuery
4. Esta es una característica de jQuery array.length es una característica de ECMAScript1.
5

Sus navegadores compatibles son -:

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

Sus navegadores compatibles son -:

Chrome, Internet Explorer, Firefox, Safari, Ópera, Microsoft Edge

Publicación traducida automáticamente

Artículo escrito por karthi03 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *