# How to add leading zero to a number in JavaScript

> Learn how to add a leading zero to a number in JavaScript using padStart, so values under 10 print as 09 instead of 9, handy for clock-style displays.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2022-09-23 | Topics: [JavaScript](https://flaviocopes.com/tags/js/) | Canonical: https://flaviocopes.com/js-add-leading-zero/

I had the need to add a leading zero when the number I had was less than 10, so instead of printing "9" on the screen, I had "09". 

The use case being I wanted to display the length of a video, and `5:04` is more logical than `5:4` to say a video is 5 minutes and 4 seconds.

Here's how I did it:

```js
Math.floor(mynumber)
    .toString()
    .padStart(2, '0')
```

All of this is native to [JavaScript](https://flaviocopes.com/javascript/), using the [Math built-in library](https://flaviocopes.com/javascript-math-object/)
