var str = "5"; alert(str.lpad("0", 4)); //result "0005" var str = "10"; // note this is string type alert(str.lpad("0", 4)); //result "0010"