add leading zeros to number javascript

54

js number add zero before -

('0' + 11).slice(-2) // '11'
('0' + 4).slice(-2)  // '04'

add leading zeros to number javascript -

function pad(num, size) {
    num = num.toString();
    while (num.length < size) num = "0" + num;
    return num;
}

Comments

Submit
0 Comments