How to get the first digit of int number in Node.js

2 Answers

0 votes
function getFirstDigit(num) {
    return Number(String(num)[0]);
}

const n = 97236;
 
let first_digit = getFirstDigit(n)
 
console.log(first_digit); 
       
        
   
    
/*
run:
     
9
        
*/

 



answered Apr 16, 2024 by avibootz
0 votes
const n: number = 97236;
   
const firstDigitStr: string = String(n)[0];
   
console.log(firstDigitStr); 
         
          
     
      
/*
run:
       
"9"
          
*/

 



answered Apr 16, 2024 by avibootz
edited Apr 16, 2024 by avibootz
...