Multiline Strings
Write readable multiline text without escapes.
Multiline Strings is a free JavaScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Old Way
Before template literals, a multiline string required explicit newline escape sequences or string concatenation across lines. This was verbose and error prone.
const old = "Line 1\nLine 2\nLine 3"
console.log(old)Template Literals Span Lines
A template literal written with backticks can contain real line breaks in the source code. Every newline you type becomes a newline in the string.
The runnable example reproduces the same output using escape sequences.
const text = "First\nSecond\nThird"
console.log(text)Whitespace Is Preserved
Indentation and spaces inside a multiline template are kept exactly as written. This matters when generating formatted output.
const block = " indented\n more indented"
console.log(block)Building Paragraphs
Multiline strings are handy for paragraphs of text, email bodies, or templates. Here we assemble a short message.
const msg = "Dear user,\n\nThank you for joining.\n\nBest regards"
console.log(msg)Counting Lines
You can split a multiline string on the newline character to count or process individual lines.
const text = "a\nb\nc\nd"
const lines = text.split("\n")
console.log("Lines: " + lines.length)Joining Into Multiline
An array of lines can be joined with a newline separator to build a multiline string programmatically.
const parts = ["one", "two", "three"]
console.log(parts.join("\n"))Combining With Interpolation
Multiline templates can also contain placeholders, mixing dynamic values with line breaks. The example uses concatenation to show the result.
const name = "Sam"
const body = "Hello " + name + ",\nWelcome aboard."
console.log(body)Tabs and Special Characters
You can include tab characters with the tab escape sequence even inside multiline strings to align columns.
const table = "Name\tAge\nAlice\t30\nBob\t25"
console.log(table)Trimming Extra Lines
Multiline strings sometimes carry leading or trailing blank lines. The trim method removes surrounding whitespace including newlines.
const raw = "\n\nHello\n\n"
console.log("[" + raw.trim() + "]")Generating Code Text
Multiline strings are useful for emitting snippets of source code or configuration as text output.
const code = "function add(a, b) {\n return a + b\n}"
console.log(code)Line Length Awareness
Remember that a multiline template includes every newline you type. Accidental line breaks can introduce unexpected whitespace, so format intentionally.
const a = "no break here and here"
console.log(a.length)Quick Check
Test your multiline understanding.
Recap
Recap: Template literals allow real multiline text.
- Source newlines become string newlines
- Whitespace is preserved exactly
- Combine with interpolation for dynamic blocks
- Use split and join to process lines
console.log("Line A\nLine B")Frequently asked questions
Is the “Multiline Strings” lesson free?
Yes — the full text of “Multiline Strings” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Multiline Strings”?
Write readable multiline text without escapes. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Multiline Strings” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.