var a = '101'; var b = '11101'; function replac(a){ var z = '00000000'; var Len=a.split("").length; a=z.substring(0,8-Len)+a; return a; } a=replac(a) alert(a) b=replac(b) alert(b)