Search code examples
javascriptselectintegerfind

How do I select the nth digit in a large integer inside javascript?


When I want to select the nth character, I use the charAt() method, but what's the equivalent I can use when dealing with integers instead of string values?


Solution

  • Use String():

    var number = 132943154134;
    
    // convert number to a string, then extract the first digit
    var one = String(number).charAt(0);
    
    // convert the first digit back to an integer
    var one_as_number = Number(one);