Languages
[Edit]
EN

JavaScript - pad right number in string with zeros or spaces to get determined length

8 points
Created by:
elmer
646

In JavaScript it is possible to pad any number with some character in the following ways:

  • with simple custom method (Example 1),
  • with padEnd method placed inside String class (Example 2 and 3) - introduced in ES2017 - is not supported in older browsers.

1. Custom solution example

Edit

In this section custom solution on how to pad any characters on the right side is presented.

2. Polifill example

Edit

ECMAScript 2017 introduced a function that pads numbers.

2.1. Inline code example

Edit

In this section presented code uses copied method from the polyfill library.

2.2. Included code example

Edit

In this section presented code uses a polyfill library.

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join