How to count the number of digits in a number in Node.js

2 Answers

0 votes
const num = 9857614;
let len = Math.trunc(Math.log10(num)) + 1;
     
console.log(len);
 
 
  
  
  
/*
run:
  
7
  
*/

 



answered Jan 7, 2022 by avibootz
0 votes
const num = 9857614;
let len = num.toString().length;
     
console.log(len);
 
 
  
  
  
/*
run:
  
7
  
*/

 



answered Jan 7, 2022 by avibootz
...