var num = '11' var n = num.length; num = n < 4 ? (n == 1 ? '000'+num : (n == 2 ? '00'+num : '0'+num)) : num; alert(+num);
var str = new String(sum); while(str.length < 4) str = '0' + str;
function addZeroes(str) { return ('0000' + str).slice(-4); } alert(addZeroes('10')); alert(addZeroes('100')); alert(addZeroes('1'));