How to use reduce() function In Javascript
Here, we will use the reduce function with the array to determine the length.
// 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}`);
Output
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.
JavaScript Program to Determine the Length of an Array
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:
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.