function bin (num) { var res = ""; while(num>0) { res=num%2; num=Math.floor(num/2); } return res; } document.write(bin(6))