Home > Software design >  padStart() targetLength is not met
padStart() targetLength is not met

Time:09-23

const id = 46919;
const string = id.toString(36); // output: "107b"

console.log(string.padStart(3, '0'));

The expected output of the console.log() would be 107 since the targetLength of method padStart() is equal to 3, but the actual output is 107b which is a total of 4 characters.

Would you know why this happens please?

Edit: I misunderstood the use of this method, I thought it would cut the extra characters like slice()

CodePudding user response:

the toString(36) converts the number to base 36 from base 10, 46919 is 107b in base 36. padStart will add characters until the length is equal to the first parameter. Since 107b is already 4 characters it doesn't do anything.

CodePudding user response:

Maybe you can use parseInt() in the end of code

const id = 46919;
const string = id.toString(36);
console.log(parseInt(string.padStart(3, '0')));
//output 107
  • Related