JavaScript Strings
Learn JavaScript text: template literals, indexing and slicing with substring, the methods worth knowing, and the immutability that shapes them all.
Basic
JavaScript strings are sequences of Unicode text, written in any of three quoting styles, and like Python they are immutable: every operation returns a new string and leaves the original untouched. The language's own contribution to the craft is the template literal , backquoted text with expressions in braces, which replaced a generation of concatenation noise and is now the default way thoughtful code builds text.
Three quotes, one you should prefer
|
|
Single and double for simple cases; backticks for interpolation.
Template literals interpolate any expression, not just variables, keep newlines you type inside them, and compose with methods like
.toFixed(2) directly inside the braces. The habit worth forming: when a string contains a value, reach for backticks first, and concatenate only when gluing two complete strings.
Reading characters and slices
Indexing with brackets reads a character,
word[0], and
word.at(-1) reaches from the end, the modern answer to the off-by-one gymnastics of
word[word.length - 1]. Slicing belongs to
slice(start, end), end-exclusive exactly like Python, with negative indexes allowed:
word.slice(0, 4),
word.slice(-5). The older
substring swaps reversed arguments instead of going negative, a historical artifact worth recognizing in old code and avoiding in new.
The methods worth memorizing
Expression | Result | Job |
|---|---|---|
"Sourdough".toLowerCase() | 'sourdough' | Case folding for comparisons. |
" hi ".trim() | 'hi' | Trimming edges, the first step for any user input. |
"a,b,c".split(",") | ['a','b','c'] | Text into an array. |
['a','b','c'].join(', ') | 'a, b, c' | Array into text. |
"dough".includes("oug") | true | Membership, no indexOf comparisons. |
"no no".replaceAll("no", "yes") | 'yes yes' | Every occurrence. |
word.length | 9 | A property, not a method; no parentheses. |
Two details earn a second look.
length is a property, so
word.length() is an error, and the counting is in UTF-16 code units, which equals characters for ordinary text and needs more care for emoji and some scripts. And comparisons of user-typed text should fold case before comparing,
a.toLowerCase() === b.toLowerCase(), because equality is exact and humans are not.
Immutability, same as Python's
Assignment to a position fails silently rather than loudly:
word[0] = "S" does nothing at all, no error, no change. Methods that appear to modify,
toUpperCase,
trim, return new strings, and the classic beginner bug is calling one and dropping its answer.
|
|
The call always returns; the assignment is your half of the bargain.
Work some text
The example below cleans a messy order line with the lesson's tools, writing results to the preview's console bridge. Run it, then make the immutability bug on purpose: change
line = line.trim() to
line.trim() alone and watch every later line of output quietly revert to the mess, exactly the kind of silent failure brackets would have caught.
Output
Ready to run.
What to notice
The
mapcall trimmed every item of the split in one expression; it gets a lesson of its own with arrays.Template literals carried the labels and the values together, with no concatenation to read backwards.
Brackets never appeared in the output code; everything used a method that returned.
Quick check
If you can slice with either
slice or
at without hesitation, explain why
name.toUpperCase() alone changes nothing, and prefer template literals when values join text, strings are settled. Next, booleans and conditionals. When you are ready, take the JavaScript Strings quiz.
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.