
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Counting elements of an array using a recursive function in JS?
The recursive function calls itself with some base condition. Let’s say the following is our array with marks −
var listOfMarks=[56,78,90,94,91,82,77];
Following is the code to get the count of array elements −
Example
function countNumberOfElementsUsingRecursive(listOfMarks) { if (listOfMarks.length == 0) { return 0; } return 1 + countNumberOfElementsUsingRecursive(listOfMarks.slice(1)); } var listOfMarks=[56,78,90,94,91,82,77]; console.log("The array="); console.log(listOfMarks); var numberOfElements=countNumberOfElementsUsingRecursive(listOfMarks); console.log("The Number of elements = "+numberOfElements);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo110.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo110.js The array=[ 56, 78, 90, 94, 91, 82, 77 ] The Number of elements = 7
- Related Articles
- Using a recursive function to capitalize each word in an array in JavaScript
- Counting unique elements in an array in JavaScript
- Counting below / par elements from an array - JavaScript
- Counting frequencies of array elements in C++
- Function for counting frequency of space separated elements in JavaScript
- Counting the occurrences of JavaScript array elements and put in a new 2d array
- Counting elements in two arrays using C++
- Using merge sort to recursive sort an array JavaScript
- Convert JS array into an object - JavaScript
- Counting number of triangle sides in an array in JavaScript
- Recursive Programs to find Minimum and Maximum elements of array in C++
- Counting cross lines in an array in C++
- Rank of All Elements in an Array using C++
- Equalize an array using array elements only in C++
- Counting possible APs within an array in JavaScript

Advertisements