javascript

Why do I get "padStart is not a function" when using padStart on numbers?


let m = 5;
m = m.padStart(2, '0');

Error:

m.padStart is not a function

Expecting result: 05;

I'm on Chrome, last version.

Any help?


Solution

  • The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start (left) of the current string.

    It is a String function. Not a number function. Refer

    Solution-

    let m = '5';
    m = m.padStart(2, '0');
    alert(m)