Show a leading zero if a number is less than 10 [duplicate] Show a leading zero if a number is less than 10 [duplicate] javascript javascript

Show a leading zero if a number is less than 10 [duplicate]


There's no built-in JavaScript function to do this, but you can write your own fairly easily:

function pad(n) {    return (n < 10) ? ("0" + n) : n;}

EDIT:

Meanwhile there is a native JS function that does that. See String#padStart

console.log(String(5).padStart(2, '0'));