javascript / beginner
Snippet
Cleaning Strings with .trim()
When handling user input in Node.js, strings often come with accidental leading or trailing whitespace. The .trim() method removes these spaces, ensuring cleaner data processing.
snippet.js
javascript
1
2
3
4
const username = " coding_wizard ";const cleanName = username.trim();console.log(cleanName); // "coding_wizard"
nodejs
Breakdown
1
const username = " coding_wizard ";
A string containing extra spaces at both ends.
2
username.trim()
Returns a new string with the whitespace removed from both start and end.