Finding the length of an array in JavaScript refers to the process of determining the number of elements contained within an array.
Below are the approaches used to Determine the length of an array in JavaScript:
Table of Content
Using length Property to Determine the Length of an Array
Here, we will use the length property of the array to determine the length of the array. It will return the number of elements in an array.
Using length Property to find the Length of an Array Example:
Here, we have used the length() property to determine the length of an array.
// Defining an number array let arr = [12, 4.7, 9, 10] // Printing the length using length property console.log ( "The length of the array: " + arr.length)
|
The length of the array: 4
Explanation:
The code initializes an array of numbers. It then uses the length
property of the array to determine its size and prints the result, indicating the number of elements in the array.
Using for of and for in loop to Determine the Length of an Array
Here, we will use the for of and for in loop and traverse each element of array and will increase size variable on each iteration by one.
Using for of and for in loop to find the Length of an Array Example:
Here, we have used for of and for in loop to determine the length of an array.
// Defining the array let arr = [12, 4.7, 9, 10] // Defining the length variable let size = 0; // Looping using for-in loop for (let element in arr) {
size++; } // Printing the length console.log(`Length by using for in loop -> ${size}`)
// Reseting the value of variable size = 0; // Looping using for-or loop for (let element of arr) {
size++; } // Printing the length console.log(`Length by using for of loop -> ${size}`)
|
Length by using for in loop -> 4 Length by using for of loop -> 4
Explanation:
The above code defines an array arr
and initializes a variable size
to 0. It then iterates over the elements of the array using both for-in
and for-of
loops, incrementing size
for each element. Finally, it prints the lengths obtained from both loops.
Using reduce() function to Determine the Length of an Array
Here, we will use the reduce function with the array to determine the length.
Using reduce() function to find the Length of an Array Example:
Here, we have used reduce() function to determine the length of an array.
// Defining the array let arr = [12,4.7,9,10]; // Using the reduce function to count the elements let length=arr.reduce((size)=>size+1,0); // Printing the Length console.log(`length of given array -> ${length}`); |
length of given array -> 4
Explanation:
The above code defines an array arr
. It then utilizes the reduce()
function to count the number of elements in the array by incrementing a counter (size
) for each element. Finally, it prints the length of the array.
Using Object.keys() method to Determine the Length of an Array
The Object.keys()
method in JavaScript is used to retrieve an array of the enumerable property names of an object. It returns an array containing the keys of the object.
Using Object.keys() method to find the Length of an Array Example :
Here, we have used theObject.keys method to determine the length of an array.
const array = [1, 2, 3, 4, 5]; const length = Object.keys(array).length; console.log(length); // Output: 5
|
5
Explanation:
The above code initializes an array array
containing elements. It converts the array indices into keys using Object.keys()
method and then calculates the length of the resulting array of keys. Finally, it prints the length of the original array.
JavaScript Program to Determine the Length of an Array – Use Cases:
Below are the Use case for JavaScript Program to Determine the Length of an Array:
1. JavaScript Array length Property
The array length property in JavaScript is used to set or return the number of elements in an array.
2. How to Find the Length of an Array in JavaScript ?
Below are the ways to find the length of an array in JavaScript:
- Using the length Property
- Looping Through the Array
- Using forEach Method
- Using Spread Operator
- Conversion to String
3. How to get the size of an array in JavaScript ?
The JavaScript Array Length returns an unsigned integer value that represents the number of elements present in the array. The value is non-negative and always a 32-bit integer.