Member-only story
Strings are a fundamental part of programming in JavaScript. Whether you're handling user input, processing data, or formatting output, mastering string manipulation techniques can significantly enhance your coding efficiency. In this guide, we'll explore essential JavaScript string manipulation techniques that every developer should know.
Not a Medium member? Read this article here
1. Trimming Whitespace
const str = ' Hello World! ';
const trimmed = str.trim();
// Output: 'Hello World!'
Removes whitespace from both ends of a string. Use trimStart() or trimEnd() to remove whitespace from the start or end only.
2. Converting to Upper or Lower Case
const str = 'Hello World!';
const upper = str.toUpperCase();
const lower = str.toLowerCase();
// Output: upper = 'HELLO WORLD!', lower = 'hello world!'
Convert all characters in a string to uppercase or lowercase. Useful for case-insensitive comparisons.
3. Splitting a String into an Array
const str = 'apple,banana,cherry';
const arr = str.split(',');
// Output: arr = ['apple', 'banana', 'cherry']
Splits a string into an array of substrings based on a specified delimiter.