How to count number of data types in an array in JavaScript ? Last Updated : 23 Jun, 2023 Comments Improve Suggest changes Like Article Like Report Given an array and the task is to count the number of data types used to create that array in JavaScript. Example: Input: [1, true, "hello", [], {}, undefined, function(){}] Output: { boolean: 1, function: 1, number: 1, object: 2, string: 1, undefined: 1 } Input: [function(){}, new Object(), [], {}, NaN, Infinity, undefined, null, 0] Output: { function: 1, number: 3, object: 4, undefined: 1 } Approach 1: In this approach, We use the Array.reduce() method and initialize the method with an empty object. JavaScript // JavaScript program to count number of // data types in an array let countDtypes = (arr) => { return arr.reduce((acc, curr) => { // Check if the acc contains the // type or not if (acc[typeof curr]) { // Increase the type with one acc[typeof curr]++; } else { /* If acc not contains the type then initialize the type with one */ acc[typeof curr] = 1 } return acc }, {}) // Initialize with an empty array } let arr = [function () { }, new Object(), [], {}, NaN, Infinity, undefined, null, 0]; console.log(countDtypes(arr)); Output{ function: 1, object: 4, number: 3, undefined: 1 } Approach 2: In this approach, we use the Array.forEach() method to iterate the array. And create an empty array and at every iteration, we check if the type of present iteration present in the newly created object or not. If yes then just increase the type with 1 otherwise create a new key by the name of the type and initialize with 1. JavaScript // JavaScript program to count number of // data types in an array let countDtypes = (arr) => { let obj = {} arr.forEach((val) => { // Check if obj contains the type or not if (obj[typeof val]) { // Increase the type with one obj[typeof val]++; } else { // Initialize a key (type) into obj obj[typeof val] = 1; } }) return obj } let arr = [function () { }, new Object(), [], {}, NaN, Infinity, undefined, null, 0 ]; console.log(countDtypes(arr)); Output{ function: 1, object: 4, number: 3, undefined: 1 } Comment More infoAdvertise with us Next Article How to count number of data types in an array in JavaScript ? devi_johns Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Methods JavaScript-Questions +2 More Similar Reads How to convert Number to Boolean in JavaScript ? We convert a Number to Boolean by using the JavaScript Boolean() method and double NOT operator(!!). A JavaScript boolean results in one of two values i.e. true or false. However, if one wants to convert a variable that stores integer â0â or â1â into Boolean Value i.e. "false" or "true". Below are 2 min read Convert a Number to a String in JavaScript These are the following ways to Convert a number to a string in JavaScript:1. Using toString() Method (Efficient and Simple Method)This method belongs to the Number.Prototype object. It takes an integer or a floating-point number and converts it into a string type.JavaScriptlet a = 20; console.log(a 1 min read Convert a negative number to positive in JavaScript In this article, we will see how we can convert a negative number to a positive number in JavaScript by the methods described below. Below are the methods to convert a negative number to a positive in JavaScript: Table of Content Multiplying by -1Using Math.abs()adding a minus signFlipping the bitUs 4 min read Check a Number is Prime or Not Using JavaScript A prime number is a whole number greater than 1, which has no positive divisors other than 1 and itself. In other words, prime numbers cannot be formed by multiplying two smaller natural numbers. For example:2, 3, 5, 7, 11, and 13 are prime numbers.4, 6, 8, 9, and 12 are not prime numbers because th 5 min read JavaScript - Convert a Number into JS Array You have a number, like 12345, and you need to convert it into an array where each element represents a digit of the number. For example, 12345 should become [1, 2, 3, 4, 5]. How can you achieve this in JavaScript?In JavaScript, there are various ways to transform a number into an array of its digit 3 min read How to generate a n-digit number using JavaScript? The task is to generate an n-Digit random number with the help of JavaScript. You can also generate random numbers in the given range using JavaScript. Below are the approaches to generate a n-digit number using JavaScript: Table of Content Using Math.random()Math.random() Method and .substring() Me 2 min read How to get decimal portion of a number using JavaScript ? Given a float number, The task is to separate the number into integer and decimal parts using JavaScript. For example, a value of 15.6 would be split into two numbers, i.e. 15 and 0.6 Here are a few methods discussed. These are the following methods: Table of Content Javascript String split() Method 3 min read Generate Random Number in Given Range Using JavaScript Here are the different ways to generate random numbers in a given range using JavaScript1. Using Math.random(): basic ApproachThis is the simplest way to generate a random number within a range using Math.random().JavaScriptlet min = 10; let max = 20; let random = Math.floor(Math.random() * (max - m 3 min read How to convert long number into abbreviated string in JavaScript ? We are given a long number and the task is to convert it to the abbreviated string(eg.. 1234 to 1.2k). Here 2 approaches are discussed with the help of JavaScript.Approaches to Convert Long Number to Abbreviated String:Table of ContentUsing JavaScript methodsUsing Custom functionUsing logarithmsUsin 6 min read How to check first number is divisible by second one in JavaScript ? Given two numbers and the task is to check the first number is divisible by the second number or not with the help of JavaScript. Before getting into the coding part, first let us know about the modulo operator and triple equals. To find a number is divisible by another or not, we simply use the rem 2 min read Like