generate 4 digit random number using substring

That's because a is a number, not a string. What you probably want to do is something like this:

var val = Math.floor(1000 + Math.random() * 9000);
console.log(val);
  • Math.random() will generate a floating point number in the range [0, 1) (this is not a typo, it is standard mathematical notation to show that 1 is excluded from the range).
  • Multiplying by 9000 results in a range of [0, 9000).
  • Adding 1000 results in a range of [1000, 10000).
  • Flooring chops off the decimal value to give you an integer. Note that it does not round.

General Case

If you want to generate an integer in the range [x, y), you can use the following code:

Math.floor(x + (y - x) * Math.random());

This will generate 4-digit random number (0000-9999) using substring:

var seq = (Math.floor(Math.random() * 10000) + 10000).toString().substring(1);
console.log(seq);