DEV Community

Cover image for Unleash the Power of JavaScript String Methods - Your Ultimate Cheat Sheet 🤯🔥

Unleash the Power of JavaScript String Methods - Your Ultimate Cheat Sheet 🤯🔥

Ram Maheshwari ♾️ on February 01, 2023

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...
Collapse
 
johongirr profile image
Jaxongir

Is Elon Musk really tweeted that?

Collapse
 
alvaromontoro profile image
Alvaro Montoro

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

Collapse
 
johongirr profile image
Jaxongir

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

Thread Thread
 
alvaromontoro profile image
Alvaro Montoro

Sorry. I'm really dense today. 😓

Collapse
 
rammcodes profile image
Ram Maheshwari ♾️ • Edited

xDDD

Collapse
 
shammisharma profile image
shammisharma

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

Collapse
 
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 ❤️

Collapse
 
shammisharma profile image
shammisharma

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

Collapse
 
georgehvm profile image
George Henrique

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

Collapse
 
kolben profile image
Jonas Kolben
Collapse
 
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]

Collapse
 
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 🙌

Collapse
 
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'
Enter fullscreen mode Exit fullscreen mode
Collapse
 
vulcanwm profile image
Medea

nice!

Collapse
 
rammcodes profile image
Ram Maheshwari ♾️

Thank You 🙌

Collapse
 
juliocamposswork profile image
Julio Campos • Edited

Thanks a lot for the advice,

Collapse
 
rammcodes profile image
Ram Maheshwari ♾️

No problem 🙌

Collapse
 
kolben profile image
Jonas Kolben
Collapse
 
z10n profile image
T

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

Collapse
 
kolben profile image
Jonas Kolben

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

Collapse
 
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 👍