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.
<!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> |
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.
<!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> |
Output:
Length of the array is: 51
Example 3: This example display how array.length property works when index key is non numeric.
<!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
Recommended Posts:
- Difference between CSS and JavaScript
- Difference between JavaScript and Php
- Difference between var and let in JavaScript
- Difference between Java and JavaScript
- Difference between TypeScript and JavaScript
- Difference between Python and JavaScript
- Difference between JavaScript and C Sharp
- Difference between == and === operator in JavaScript
- What's the difference between JavaScript and JScript?
- Difference between != and !== operator in JavaScript
- What is the difference between every() and some() methods in JavaScript ?
- Difference between alert box and confirmation box in JavaScript
- Compute the set difference using JavaScript arrays
- What is the difference between call and apply in JavaScript?
- Difference between stopPropagation vs stopImmediatePropagation in JavaScript
- Difference between DOM parentNode and parentElement in JavaScript
- Difference between substr() and substring() in JavaScript
- What is the difference between children and childNodes in JavaScript?
- What is the difference between freeze and seal in JavaScript ?
- Get the relative timestamp difference between dates in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : karthi03


