0
0
JavascriptHow-ToBeginner · 3 min read

How to Find Length of Array in JavaScript Quickly

To find the length of an array in JavaScript, use the .length property on the array variable. For example, array.length returns the number of elements in the array.
📐

Syntax

The syntax to get the length of an array is simple:

  • array.length: Returns the number of elements in the array.

Here, array is your array variable, and .length is a property that gives you the size.

javascript
const array = [1, 2, 3, 4];
const length = array.length;
💻

Example

This example shows how to find and print the length of an array with 4 elements.

javascript
const fruits = ['apple', 'banana', 'cherry', 'date'];
console.log('Number of fruits:', fruits.length);
Output
Number of fruits: 4
⚠️

Common Pitfalls

Some common mistakes when finding array length:

  • Using parentheses like a function: array.length() is wrong because length is a property, not a function.
  • Trying to get length of a non-array variable will give undefined or errors.
  • Remember that length counts all elements, including undefined or empty slots.
javascript
const arr = [1, 2, 3];
// Wrong: arr.length() - this causes an error
// Correct:
console.log(arr.length);
Output
3
📊

Quick Reference

Summary tips to remember:

  • Use array.length without parentheses.
  • Works on any array to get its size.
  • Length updates automatically if you add or remove elements.

Key Takeaways

Use the .length property to get the number of elements in an array.
Do not use parentheses with .length; it is not a function.
The length property updates automatically when the array changes.
Length counts all elements, even if some are undefined or empty.
Accessing length on non-arrays may cause errors or unexpected results.