add leading zeros in javascript code example
Example 1: js number add zero before
('0' + 11).slice(-2) // '11'
('0' + 4).slice(-2) // '04'
Example 2: javascript print int with leading zeros
function padLeadingZeros(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}
padLeadingZeros(57, 3);// "057"
padLeadingZeros(57, 4); //"0057"
Example 3: js num to string with leading 0
String(<number>).padStart(<places>, '0')
String(42).padStart(5, '0'); // '00042'