Padding, Trimming, and Formatting Strings
Presenting data to users often requires normalising whitespace or aligning values into columns. JavaScript provides a small but complete set of string methods for these tasks, available on all modern browsers and Node.js.
Trimming Whitespace
trim() removes leading and trailing whitespace (spaces, tabs, newlines). trimStart() and trimEnd() remove whitespace from only one side, which is useful when you want to preserve intentional indentation on one end.
Padding
padStart(targetLength, padString) prepends the pad string until the string reaches targetLength. padEnd appends instead. Both methods are commonly used to zero-pad numbers or align tabular output. If the pad string is longer than one character, it is repeated and then truncated to fit.
Repeat
repeat(count) returns a new string containing the original repeated count times. It is handy for generating separators, test data, or visual indentation.
Case Conversion
toUpperCase() and toLowerCase() convert all alphabetic characters. toLocaleUpperCase() and toLocaleLowerCase() respect locale-specific rules, important for languages like Turkish where case rules differ from ASCII.