JavaScript

Array.size() vs Array.length – JavaScript

In JavaScript, the “length” is a property of an array object that refers to the total number of array elements. On the other hand, “size()” is a method available in some programming languages for collections, such as lists, sets, and maps. It returns or outputs the number of elements present in the selected collection.

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:

array.length

Or use it with the bracket notation:

[array.length]

Example

Create an array of even numbers:

var array = [2, 4, 6, 8, 10, 12, 14];

Determine the size of an array using the length property and store it in a variable “size”:

var size = array.length;

Lastly, print the length or size of the array:

console.log(size);

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:

var size = array.size();

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.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.