This post is originally published to my blog.
In this post I will summarize 10 javascript string methods you should at least known according to me...
For further actions, you may consider blocking this person and/or reporting abuse
repeat
exists? :oI've been doing
new Array(52).join('a');
to make a string of 52a
s, but I could have just used'a'.repeat(52)
... now I need to make a little commit to update that.The other day I even ranted about how nice Python's
'a' * 52
would have been.They are not same
Oops, you're right! Typo on my part :) For the sake of my E2E tests, I actually think
repeat
would be easier to read.Actually repeat is easy to read
If you want to make string of 52 a's with repeat is so easy
Array index starts from 0 which means it will print 52 times.
Yass, and since join acts as 'glue', using 'a' as glue for 52 nothings will print 'a' 51 times. Give it a try ;)
Great list of methods for Strings. I would just like to highlight that String.startsWith() and String.repeat() are ES2015+ String.endsWith() is ES6+, so polyfill accordingly to support edge cases.
Thanks, was wondering about that!
Very useful, Thanks!
Thanks for this!
Hi, thanks for the update. What about substr() function. Is it necessary when slice() exist?
substr() is not part of the core JavaScript language and may be removed in the future. Use slice() or substring() instead
I'm not sure whether this one is deprecated or maybe I read that incorrectly...
Can you tell me real world use case of reapeat() method? Idk why we need it 🤔
Thanks in advance!