The JavaScript String object
The String object represents text. Create one with quotes or backticks — "hi", `Hi ${name}`. Strings come with methods for searching (includes()), slicing (slice()), splitting (split()) and transforming (replace()). They're immutable, so methods return new strings.
Overview
The String is how JavaScript handles text. You create strings with single quotes, double quotes, or backticks — and backticks unlock template literals, which let you embed values with ${...} and span multiple lines: `Hello ${name}, you have ${count} messages`. That's the modern, readable way to build text, and it has largely replaced clunky + concatenation.
Strings carry a rich set of methods. Search them with includes(), indexOf() and startsWith(); extract parts with slice(); break them apart with split(); clean them with trim(); and transform them with replace(), toUpperCase() and toLowerCase(). The length property gives the character count.
The key thing to internalize is that strings are immutable — you can't change a character in place. Every method that "modifies" a string actually returns a new one, leaving the original untouched, so you must use the return value. Individual characters are read by index (str[0]) but can't be assigned to. For case-insensitive comparison, normalize both sides with toLowerCase() first.
Syntax
const a = "double";
const b = 'single';
const c = `template ${a}`; // backticks: interpolation + multiline
"hello".length; // 5
"hello"[0]; // "h"
"hello".toUpperCase(); // "HELLO" (new string)
Example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
const name = 'Ada';
const count = 3;
const message = `Hi ${name}, you have ${count} new ${count === 1 ? 'message' : 'messages'}.`;
document.getElementById('out').textContent = message;
</script>
Best practices
- Use template literals (backticks) for interpolation and multi-line text instead of
+concatenation. - Remember strings are immutable — always use the value a method returns.
- Normalize case with toLowerCase() before comparing or searching.
- Reach for slice() over the older
substr()for extracting parts.
Frequently asked questions
How do I create a string in JavaScript?
"text", 'text', or a template literal `Hi ${name}` for interpolation.Are strings mutable in JavaScript?
How do I get the length of a string?
length property: str.length.What is a template literal?
${value}) and multi-line text — the modern way to build strings.