This article will describe the difference between size() and Array.length in JavaScript.
What is JavaScript Array.length Property?
“length” is the property of an array object. It is a read-only property of an array and can be used to determine the size or length of the array or to access the last element in the array. It can be accessed with the help of dot notation or bracket notation.
How to Use Array.length in JavaScript?
For using the length property of the array object to determine the size or length of an array, follow the given syntax:
Or use it with the bracket notation:
Example
Create an array of even numbers:
Determine the size of an array using the length property and store it in a variable “size”:
Lastly, print the length or size of the array:
Output
What is size() in JavaScript?
“size()” is a JavaScript method utilized to determine or find out the size or an object’s length. It is also used for collections, such as “lists”, “sets”, and “maps”. However, it is not available for the array objects.
Example
Call the size() method with the defined array:
It gives an error “array.size is not a function” because the size() method is not available for the array:
That’s all about the array.size() and the array.length in JavaScript.
Conclusion
“size()” is a method available for collections like sets, lists, and maps. At the same time, “Array.length” is a property of an array object that represents the total number of elements in an array or the size/length of an array. However, the “length” property is significantly faster than a method call. This article described the difference between Array.size() and Array.length in JavaScript.