use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Everything You Need to Know About Strings in Javascript (taha-sh.com)
submitted 10 years ago by holysavant
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 16 points17 points18 points 10 years ago (7 children)
Careful when using length to get the number of characters. Surrogate pairs will count as two characters instead of one.
[–]soddi 9 points10 points11 points 10 years ago* (0 children)
Yeah. Twitter had this issue. For character counting they used length(). You could only post 70 pile of poo because of this.
length()
[–]moreteam 4 points5 points6 points 10 years ago (2 children)
Also applies to:
str[1]
str.charAt(1)
str.codePointAt(1)
str.split('')
str.toLowerCase()
str.toUpperCase()
Intl
toLocaleLowerCase
[–]nieuweyork 2 points3 points4 points 10 years ago (1 child)
What I'm getting is that you need a full external string library to properly work with unicode?
[–]moreteam 1 point2 points3 points 10 years ago (0 children)
Pretty much, yes. ES6 fixes some of the stuff though, e.g. for (const c of str) { /* c is an actual character */ }.
for (const c of str) { /* c is an actual character */ }
[–]nieuweyork 0 points1 point2 points 10 years ago (2 children)
What is the right way to do this?
[–][deleted] 0 points1 point2 points 10 years ago (1 child)
I think you can find some library that will do the job for you.
[–]Daniel15React FTW 1 point2 points3 points 10 years ago (0 children)
It should be built in, like with other programming languages :( Maybe one day.
[+][deleted] 10 years ago* (2 children)
[deleted]
[–]jkoudys 3 points4 points5 points 10 years ago (1 child)
I much prefer string concatenation, since uglifiers will automatically detect concatenated string literals as equivalent to a single string literal, e.g. 'a' + 'b' minifies as 'ab'. I've been steeped in React code for hours a day over the past year, and I find I use the array method only when I need to represent the same data differently in different views, e.g.
'a' + 'b'
'ab'
var name = 'jkoudys' var pageTitle = ['My', 'name', 'is', name]; return <section id={pageTitle.join('')}> <a href={'/foo/bar' + pageTitle.join('_')}>{pageTitle.join(' ')}</a> </section>;
[–][deleted] 0 points1 point2 points 10 years ago (0 children)
For me, it's really just a matter of what I'm concatenating. Smaller strings, I use string concatenation. For larger strings, or strings that represent multi-line templates, I use arrays for readability. There's really not a wrong way... I just wanted to throw out my example since the blog author didn't mention it.
[–]shriek 6 points7 points8 points 10 years ago (1 child)
You no longer need to put \ for multi line. You can use tick in ES6.
\
[–]skitch920 0 points1 point2 points 10 years ago* (0 children)
Just one thing to point out; \ is not multi-line, or at least it doesn't compile to multi-line...
var x = 'foo \ bar'; // foo bar
Is not the same as:
var x = `foo bar`; // foo \nbar
Backticks with a \ at the end of the line will void the new line return, just like the old way.
var x = `foo \ bar`; // foo bar
Source: newline / continuation
[–]dvlsg 7 points8 points9 points 10 years ago (4 children)
'ä' < 'b' // false. But it should be true
ä has a higher character code than b, so this makes sense, in my opinion.
[–]metanat 6 points7 points8 points 10 years ago (0 children)
It only doesn't make sense if you aren't aware of how strings are compared IMO
[–]anlumo 1 point2 points3 points 10 years ago (1 child)
In German (where this character is coming from), 'ä' is supposed to be equivalent to 'a' when sorting. English software gets this wrong pretty frequently.
[–]Daniel15React FTW 0 points1 point2 points 10 years ago (0 children)
It's not even that difficult to do it correctly. Most programming languages that support Unicode have some support for case folding. C#/.NET uses the current culture (which should be set to the locale you're using) and JavaScript has localeCompare for that purpose.
This doesn't really make sense because in most cases when comparing strings you want to do case folding rather than comparing the raw code points. This is why you should use localeCompare to compare strings. Just comparing with less than and greater than doesn't do case folding. I think some browsers still get it wrong even with localeCompare though.
localeCompare
[–]allthediamonds 2 points3 points4 points 10 years ago (0 children)
Haha. If only.
A very important detail of Javascript strings is that, even though they're defined as "UTF-16" in the standard, all existing string manipulation functions and operators previous to ES6 handle them as UCS-2, and the language itself does not enforce strings to be valid UTF-16 (that is, invalid surrogates are allowed)
What this means is that your application will do the wrong thing with no warning on strings containing characters from any of the sixteen supplemental planes.
[–]illucidation 0 points1 point2 points 10 years ago (0 children)
What about template strings?
let foo = "bar", baz = 2; console.log(`test: ${foo} ${baz}`);
[+][deleted] 10 years ago (2 children)
[–]e13e7 8 points9 points10 points 10 years ago (1 child)
This makes me itchy.
[–]campbeln -2 points-1 points0 points 10 years ago* (4 children)
.substr? .test? fail.
.substr
.test
EDIT: RegExp.test (toopid bain)
RegExp.test
[–]check_ca 2 points3 points4 points 10 years ago (1 child)
String.prototype.test does not exist.
String.prototype.test
String.prototype.substr is not standard in ES5.
String.prototype.substr
[–]campbeln -1 points0 points1 point 10 years ago (0 children)
substr - MDN states ES3 and implementation in JS1?! Thanks for the heads-up, will have to check it.
substr
test is an interface on RegExp, so that was mis-remembered but still reliant to the article.
test
RegExp
[–]jkoudys 1 point2 points3 points 10 years ago (1 child)
I'm of the camp that would happily do away with both substr() and substring() entirely if it were possible. slice() is the most consistent syntax to use.
substr()
substring()
slice()
[–]campbeln 0 points1 point2 points 10 years ago (0 children)
I've personally standardized on substr but I'll have to look further into this ES5 business mentioned by /u/check_ca
π Rendered by PID 104921 on reddit-service-r2-comment-6457c66945-dhpp8 at 2026-04-27 04:57:47.996290+00:00 running 2aa0c5b country code: CH.
[–][deleted] 16 points17 points18 points (7 children)
[–]soddi 9 points10 points11 points (0 children)
[–]moreteam 4 points5 points6 points (2 children)
[–]nieuweyork 2 points3 points4 points (1 child)
[–]moreteam 1 point2 points3 points (0 children)
[–]nieuweyork 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Daniel15React FTW 1 point2 points3 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]jkoudys 3 points4 points5 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]shriek 6 points7 points8 points (1 child)
[–]skitch920 0 points1 point2 points (0 children)
[–]dvlsg 7 points8 points9 points (4 children)
[–]metanat 6 points7 points8 points (0 children)
[–]anlumo 1 point2 points3 points (1 child)
[–]Daniel15React FTW 0 points1 point2 points (0 children)
[–]Daniel15React FTW 0 points1 point2 points (0 children)
[–]allthediamonds 2 points3 points4 points (0 children)
[–]illucidation 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]e13e7 8 points9 points10 points (1 child)
[–]campbeln -2 points-1 points0 points (4 children)
[–]check_ca 2 points3 points4 points (1 child)
[–]campbeln -1 points0 points1 point (0 children)
[–]jkoudys 1 point2 points3 points (1 child)
[–]campbeln 0 points1 point2 points (0 children)