DEV Community

Cover image for Unleash the Power of JavaScript String Methods - Your Ultimate Cheat Sheet ๐Ÿคฏ๐Ÿ”ฅ
Ram Maheshwari โ™พ๏ธ
Ram Maheshwari โ™พ๏ธ

Posted on Edited on

Unleash the Power of JavaScript String Methods - Your Ultimate Cheat Sheet ๐Ÿคฏ๐Ÿ”ฅ

JavaScript strings are a powerful tool for manipulating text and working with data. To get the most out of your strings, you'll want to familiarize yourself with the various string methods available in JavaScript ๐Ÿ”ฅ

In this article, I'll provide you with an ultimate cheat sheet for JavaScript string methods, making it easy to find exactly what you need to accomplish your goals ๐Ÿš€

With clear code examples and descriptions, you'll learn how to use each method to perform common string operations like finding substrings, converting case, and removing whitespace ๐Ÿ˜Ž

Whether you're a seasoned JavaScript developer or just starting out, this cheat sheet will be a valuable resource for optimizing your string-based code โšก

So, Let's get started ๐Ÿค–


1) str.length : returns the length of a string.

JS str.length example


2) str.charAt(index) : returns the character at the specified index in a string.

JS str.charAT(index) example


3) str.indexOf(searchValue[, fromIndex]) : returns the index of the first occurrence of the specified value in a string.

JS str.indexOf(searchValue[, fromIndex] example


4) str.lastIndexOf(searchValue[, fromIndex]) : returns the index of the last occurrence of the specified value in a string.

JS str.lastIndexOf(searchValue[, fromIndex] example


5) str.substring(start[, end]) : returns a new string that is a substring of the original string.

JS substring(start[, end]) example


6) str.substr(start[, length]) : returns a new string that is a part of the original string.

JS str.substr(start[, length]) example


7) str.toLowerCase() : returns a new string with all characters in lower case.

JS str.toLowerCase() example


8) str.toUpperCase() : returns a new string with all characters in upper case.

JS str.toUpperCase() example


9) str.trim() : removes whitespace from both ends of a string

JS str.trim() example


10) str.replace(searchValue, replaceValue) : replaces all occurrences of the specified value in a string with another value.

JS str.replace(searchValue, replaceValue) example


Hope this is helpful โœจ

Do Like โค๏ธ & Save ๐Ÿ”–


Do ๐—™๐—ผ๐—น๐—น๐—ผ๐˜„ me on Linkedin for more:
Tips๐Ÿ’ก+ Guides๐Ÿ“œ + Resources โšก related to Programming and Web Development ๐Ÿ‘จโ€๐Ÿ’ป

Ram Maheshwari (@rammcodes) Linkedin


Do Follow me here on dev.to โœ…

Top comments (21)

๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
 
johongirr profile image
Jaxongir •

Is Elon Musk really tweeted that?

๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
 
alvaromontoro profile image
Alvaro Montoro •

No. It's just clickbait from the author. All their articles follow the same (deceptive) pattern to get attention.

๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
 
johongirr profile image
Jaxongir •

It was sarcasm! But thanx for the reply! You do what you gatta do to, don't you?

๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026 Thread
 
alvaromontoro profile image
Alvaro Montoro •

Sorry. I'm really dense today. ๐Ÿ˜“

๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
 
rammcodes profile image
Ram Maheshwari โ™พ๏ธ • • Edited

xDDD

๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
 
shammisharma profile image
shammisharma •

Don't we use str.replace(/l/g, "L") to replace all l with L present in string.

๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
 
rammcodes profile image
Ram Maheshwari โ™พ๏ธ •

It uses Regx which is great but can get complicated for beginners who aren't familiar with regular expressions, otherwise, it's โค๏ธ

๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
 
shammisharma profile image
shammisharma •

yeah but then shouldn't it say "replaces FIRST occurrence of the specified value..." !

๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
 
georgehvm profile image
George Henrique •

there's a .replaceAll, but it's relatively new, i guess, so using regex is your best bet.

๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
 
kolben profile image
Jonas K •
๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
 
iambilalriaz profile image
Hafiz Muhammad Bilal •

Amazing stuff, truly liked it.
One thing I would like to mention is I think instead of str.charAt(index), It is prefer to use str[index]

๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
 
rammcodes profile image
Ram Maheshwari โ™พ๏ธ •

Thanks a lot for sharing that, It will help a lot of people, The only reason why I think charAt() is more preferred over the str[] is that str[] has poor support with some very old browsers so that's why it is recommended to use charAt() as a safe bet otherwise str[] will work just fine 99.99โ„… of the time

Again, Thank you so much for sharing this ๐Ÿ™Œ

๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
 
z10n profile image
T •

I think charAt() is more preferred over the str[] is that str[] has poor support with some very old browsers

IE7?
Is this a problem in 2023?

Btw, there is a difference between [] and charAt:

'hello'[NaN] // undefined
'hello'.charAt(NaN) // 'h'

'hello'[true] //undefined
'hello'.charAt(true) // 'e'
๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026 ๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
 
vulcanwm profile image
Medea •

nice!

๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
 
rammcodes profile image
Ram Maheshwari โ™พ๏ธ •

Thank You ๐Ÿ™Œ

๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
 
juliocamposswork profile image
Julio Campos • • Edited

Thanks a lot for the advice,

๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
 
rammcodes profile image
Ram Maheshwari โ™พ๏ธ •

No problem ๐Ÿ™Œ

๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
 
kolben profile image
Jonas K •
๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
 
z10n profile image
T •

That's why such posts are useless, it's always better to check the actual documentation.

๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
 
kolben profile image
Jonas K •

Totally agree. Reading up on strings at mdn makes much more sense. Just trying to correct the obvious issues:)

๐Ÿ“บ Info Siaran: rammcodes/unleash the power of javascript string methods your ultimate cheat sheet - Info Live Streaming 2026
 
fruntend profile image
fruntend •

ะกongratulations ๐Ÿฅณ! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up ๐Ÿ‘