JS Built-ins
StringString.prototype.padStartbeginner

String.prototype.padStart() β€” zero-pad numbers

πŸ“– Usage Example
'5'.padStart(3, '0')       // β†’ '005'
'hi'.padStart(5, '-')     // β†’ '---hi'
'hello'.padStart(3)       // β†’ 'hello'

Use padStart() to pad the beginning of a string with a fill character until it reaches the target length.

String.prototype.padStart()

str.padStart(targetLength, padString?) pads the start of str with padString (default: space) until the total length reaches targetLength.

Challenge: Implement zeroPad(n, width) that zero-pads a number to the given width.

zeroPad(5, 3)  // β†’ '005'
zeroPad(42, 4) // β†’ '0042'
πŸ’‘ Hints (1)
  • β€’ Convert to string first: `String(n).padStart(width, "0")`.