June 9, 2022 ‐ 1 min read
To specifically remove only the leading whitespaces, whitespaces at the start of the string, you can use the
.trimStart() string method.
let input = ' monday'; input = input.trimStart() console.log(input) // => 'monday'
.trimStart() keeps the original String object intact and returns a new String object with the leading spaces stripped off.
If you prefer to also remove the trailing whitespaces at the end of the string, you can just use the
.trim() method instead. This takes care of both the spaces at the beginning and end of the string.