Pad a number to two digits with Javascript
Similar to padding a number with leading zeros, we can pad a number into two digits in JavaScript.
Advertisements
function padToTwoDigits(number) { return (number < 10 ? '0' : '') + number } console.log(padToTwoDigits(9)); //09 console.log(padToTwoDigits(19)); //19