Home › Forums › JavaScript › Add '0' before single digit numbers jQuery? › Reply To: Add '0' before single digit numbers jQuery?

see here: http://jsfiddle.net/Sf62b/1/

The trick is that you have to return a string, because numbers don’t have leading zeros:

```
return (n < 9 ? "0" : "") + (n + 1);
```

The first part returns “0” if the number is less than 9, the second part appends the number, increased by one. The result is a string (string + number -> string). Note: I compare n with 9, because n+1 doesn’t need a trailing zero when n is 9.

However, on the next click, n contains “01”, so the above code would result in “0011”, because “01” < 10 and “001” + 1 = “0011”… loose type weirdness. So first, you have to cast n back to a number, like this

```
n = new Number(n);
```

or even shorter

```
n = +n
```

You could also compress all that into one statement:

```
return ((n = +n+1) < 10 ? "0" : "") + n;
```