Syntax
str.trim()
str.trimStart()
str.trimEnd()Examples
Basic Trimming
Cleaning whitespace from user input.
const input = " hello world ";
console.log(trim = input.trim()); // "hello world"
console.log(input.trimStart()); // "hello world " - only leading removed
console.log(input.trimEnd()); // " hello world" - only trailing removedValidating Trimmed Input
A common real-world pattern for form validation.
function validateUsername(input) {
const trimmed = input.trim();
if (trimmed.length === 0) {
return "Username cannot be empty or just whitespace";
}
return `Valid username: ${trimmed}`;
}
console.log(validateUsername(" ")); // "Username cannot be empty or just whitespace"
console.log(validateUsername(" alice ")); // "Valid username: alice"Best practices
- Always trim() user input before validation checks (like checking for empty strings) to catch whitespace-only submissions
- Trim input before storing it, to avoid inconsistent data caused by accidental leading/trailing spaces
- Use trimStart()/trimEnd() specifically when only one side needs cleaning, such as preserving intentional trailing formatting
- Remember trim() only removes whitespace characters (spaces, tabs, newlines), not other characters like punctuation
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
String Basics: length, charAt(), at()
Strings are immutable sequences of characters - every string method returns a new string rather than modifying the original. The length property gives the character count. charAt() returns the character at a given index, while the newer at() method also supports negative indices to count from the end.slice(), substring(), substr()
These three methods extract a portion of a string as a new string. slice() and substring() are similar, but slice() accepts negative indices (counting from the end) while substring() treats negative values as 0 and swaps arguments if start is greater than end. substr() (deprecated) uses a start index and a length rather than an end index.indexOf(), includes(), startsWith(), endsWith()
These methods search within a string for a substring. indexOf() returns the position of the first match (or -1). includes() returns a simple true/false. startsWith() and endsWith() check specifically whether the string begins or ends with a given substring. All are case-sensitive.replace() and replaceAll()
replace() returns a new string with the first match of a pattern replaced by a new value - or all matches, if the pattern is a regex with the global (g) flag. replaceAll(), introduced in ES2021, always replaces every occurrence without needing a regex, making simple find-and-replace-all operations more straightforward.
Strings are immutable sequences of characters - every string method returns a new string rather than modifying the original. The length property gives the character count. charAt() returns the character at a given index, while the newer at() method also supports negative indices to count from the end.slice(), substring(), substr()
These three methods extract a portion of a string as a new string. slice() and substring() are similar, but slice() accepts negative indices (counting from the end) while substring() treats negative values as 0 and swaps arguments if start is greater than end. substr() (deprecated) uses a start index and a length rather than an end index.indexOf(), includes(), startsWith(), endsWith()
These methods search within a string for a substring. indexOf() returns the position of the first match (or -1). includes() returns a simple true/false. startsWith() and endsWith() check specifically whether the string begins or ends with a given substring. All are case-sensitive.replace() and replaceAll()
replace() returns a new string with the first match of a pattern replaced by a new value - or all matches, if the pattern is a regex with the global (g) flag. replaceAll(), introduced in ES2021, always replaces every occurrence without needing a regex, making simple find-and-replace-all operations more straightforward.