# Strings & Template Literals

> How to work with text in JavaScript, from basic quotes to powerful multi-line template literals with injected expressions.

Subject: JavaScript | Lesson 5 of 63 | 5 min read
Interactive lesson: https://codewithanimation.in/javascript/strings-and-template-literals
Previous: [Operators & Type Coercion](https://codewithanimation.in/javascript/operators-and-coercion.md) | Next: [Numbers, BigInt & Math](https://codewithanimation.in/javascript/numbers-and-math.md)

## What are strings?

A string is a primitive data type used to represent text. In JavaScript, you can create strings using single quotes (`'hello'`), double quotes (`"hello"`), or backticks (`` `hello` ``).

Strings created with backticks are called Template Literals. They were introduced in ES6 and provide advanced features that single and double quotes lack.

## Why use Template Literals?

Before template literals, combining strings and variables (concatenation) was tedious and error-prone, requiring a mess of `+` operators and manual spaces.

Template literals allow 'interpolation', which means you can inject variables and JavaScript expressions directly into the string using `${expression}` syntax. They also naturally support multi-line strings without needing `\n` escape characters.

## String Methods

Even though strings are primitives, JavaScript automatically wraps them in a temporary object when you try to access properties on them. This gives you access to a huge library of built-in methods.

You can check the length (`str.length`), change case (`str.toUpperCase()`), extract parts (`str.slice(0, 5)`), or replace text (`str.replace('a', 'b')`).

Remember that strings are immutable. String methods never change the original string; they always return a brand new string.

## Common mistake: Zero-based indexing

Like most programming languages, JavaScript strings are zero-indexed. The first character is at index `0`, the second is at index `1`, and the last character is at `str.length - 1`. Trying to access `str[str.length]` will return `undefined`.

## Tip: Chaining methods

Because string methods return new strings, you can chain them together in a single fluid line. For example: `name.trim().toLowerCase().replace(' ', '-')`.

## Try it: Template Interpolation

Watch how a template literal effortlessly injects variables and evaluates math expressions directly inside the string.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/strings-and-template-literals

## Check yourself

### Question 1

Which quote style supports multi-line strings without escape characters?

- A. Backticks (`)
- B. Single quotes (')
- C. Double quotes (")

Correct answer: A

Template literals (backticks) inherently support multi-line strings, respecting line breaks exactly as they are written in the code.

- Why not B: Incorrect. Single quotes require `\n` for new lines.
- Why not C: Incorrect. Double quotes require `\n` for new lines.

### Question 2

What is the output of `` `2 + 2 = ${2 + 2}` ``?

- A. "2 + 2 = 4"
- B. "2 + 2 = ${2 + 2}"
- C. "4 = 4"

Correct answer: A

The `${}` syntax allows you to embed any valid JavaScript expression inside a template literal. It evaluates `2 + 2` to `4` and injects it into the surrounding text.

- Why not B: Incorrect. The expression inside `${}` is evaluated.
- Why not C: Incorrect. Only the code inside the `${}` block is evaluated as JavaScript.

### Question 3

If `const word = "Cat";`, what does `word.toUpperCase()` do to the `word` variable?

- A. Nothing, it returns a new string "CAT".
- B. It mutates the variable to "CAT".
- C. It throws an error because word is a const.

Correct answer: A

Strings are primitive values, meaning they are immutable. Methods like `toUpperCase()` do not modify the original string; they evaluate and return a completely new string.

- Why not B: Incorrect. Strings are primitive values and are strictly immutable.
- Why not C: Incorrect. Calling a method doesn't reassign the variable.

## Remember this

- Template literals use backticks (`` ` ``) and support `${}` for interpolation.
- Template literals inherently support multi-line strings.
- Strings are zero-indexed, meaning the first character is at index `0`.
- String methods never modify the original string; they return a new one.
