Multiline Strings
Write readable multiline text without escapes.
Multiline Strings is a free JavaScript Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.
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")Learn JavaScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 59
- Lessons
- 200
Frequently Asked Questions
Is the “Multiline Strings” lesson free?
Yes — any 3 lessons of the JavaScript Academy learning path, including “Multiline Strings”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
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.
All lessons in this course
- String Interpolation Basics
- Multiline Strings
- Tagged Template Functions
- Practical Tagged Template Use Cases