js pad 2 1

js pad 2

function pad2(n) {
  return (n < 10 ? '0' : '') + n; 
}
alert(pad2(3)); //03
alert(pad2(12)); //12

Here is what the above code is Doing:
1. The function pad2() takes a number as an argument.
2. The function checks if the number is less than 10.
3. If the number is less than 10, the function returns a string with a leading zero.
4. If the number is greater than or equal to 10, the function returns the number as a string.

Similar Posts