-8
var a = 000077;

I would like to see in console.log(a) 000077. How is it possible to perform this?

  • 3
    Have you tried making your variable a string? – Preston Martin Oct 13 '16 at 15:39
  • Convert it to a string and then dynamically pad it, e.g. `a.toString(8).padStart(6, '0')`. – evolutionxbox Oct 13 '16 at 15:40
  • As a note: as long as you do not use es5 or newer in strict mode then the number would be interpreted as octal when you prefix it with `0` see [MDN: Numbers](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Numbers_and_dates) – t.niese Oct 13 '16 at 15:44

2 Answers2

0

You could format the number as string with Number#toLocaleString.

var a = 77;

console.log(a.toLocaleString(undefined, { minimumIntegerDigits: 6, useGrouping: false }));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
-1

instead of

var a = 000077;

make it a string

var a = "000077";
sergio0983
  • 1,232
  • 8
  • 15