String.prototype.padLeft() - mkloubert/jsToolbox GitHub Wiki

String.prototype.padLeft(len, pad) method

Returns a string with a minimum length with padding chars at the beginning.

Syntax

String.prototype.padLeft(len, pad);

Parameters

Name Type Description
len Number The minimum length of the result string.
pad String [OPTIONAL] The custom padding string to use (is SPACE by default).

Result

The new string.

Examples

var s = '5979';

// '     5979'
var res1 = s.padLeft(9);
// '0005979'
var res2 = s.padLeft(7, '0');