You're reading for free via habtesoft's Friend Link. Become a member to access the best of Medium.

Member-only story

10 JavaScript String Tricks Every Developer Should Know

habtesoft
2 min readDec 1, 2024

JavaScript strings are more than just a way to handle text — they’re a powerful tool with plenty of built-in methods to simplify your coding tasks. Here are 10 must-know tricks to help you harness the full potential of JavaScript strings.

Not a Medium member? Read this article here

1. Reverse a String in One Line

Reversing a string can be done with a simple combination of methods:

const reverseString = str => str.split('').reverse().join('');
console.log(reverseString("hello")); // Output: "olleh"

2. Check if a String Contains Another String

Using includes, you can quickly check for substrings:

const contains = str => str.includes("world");
console.log(contains("hello world")); // Output: true

3. Convert a String to Title Case

Capitalize the first letter of each word:

const toTitleCase = str => str.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join(' ');
console.log(toTitleCase("javascript string tricks")); // Output: "Javascript String Tricks"

4. Remove Whitespace from Both Ends

Trim spaces at the start and end with trim:

const trimmed = "   hello world   ".trim();
console.log(trimmed); // Output: "hello world"

5. Repeat a String Multiple Times

Easily repeat a string using repeat:

const repeated = "hello ".repeat(3);
console.log(repeated); // Output: "hello hello hello "

6. Find the Occurrence of a Substring

Count how many times a substring appears:

const countOccurrences = (str, sub) => str.split(sub).length - 1;
console.log(countOccurrences("hello world, hello JavaScript", "hello")); // Output: 2

7. Pad a String to a Specific Length

Use padStart or padEnd to add characters to a string:

const padded = "42".padStart(5, "0");
console.log(padded); // Output: "00042"

8. Replace All Occurrences of a Substring

Replace every occurrence with replaceAll:

const updatedString = "banana".replaceAll("a", "o");
console.log(updatedString); // Output: "bonono"

For environments without replaceAll, use a global regex:

const updated = "banana".replace(/a/g, "o");

9. Extract Only Letters or Numbers

Use regex to filter specific characters:

const onlyLetters = str => str.replace(/[^a-zA-Z]/g, '');
console.log(onlyLetters("a1b2c3")); // Output: "abc"

10. Convert a String to an Array of Characters

Split a string into its individual characters:

const chars = [..."hello"];
console.log(chars); // Output: ['h', 'e', 'l', 'l', 'o']

Mastering these string tricks can significantly improve your JavaScript skills, making your code cleaner, faster, and more efficient. Experiment with these methods, and you’ll soon discover even more ways to manipulate strings effortlessly.

Let us know which trick is your favorite, or share your own tips in the comments!

You can support me here:
Buy me a coffee ☕
Thank you for being part of my journey and helping me grow! Your support makes a real difference. 🙏💙

habtesoft
habtesoft

Written by habtesoft

Passionate JavaScript developer with a focus on backend technologies. Always eager to connect and learn. Let’s talk, https://buymeacoffee.com/habtesoftat

Responses (8)

Write a response

Can you plz make it public I am not able to read

Reversing a string can be done with a simple combination of methods:

This is not a good way of doing it, as it will break if you try to reverse a string using emojis. Much better, although still not perfect, to use:
const reverseString = str => [...str].reverse().join``

Very useful, thanks alot