JavaScript Tutorial · JavaScript Basics · Lesson 8 of 44

JavaScript Strings

Create strings, join them, use template literals and escape characters.

Creating strings

Strings hold text. You can use single quotes, double quotes or backticks:

Example 1Try it yourself
const single = 'It works';
const double = "It's easy";
const backtick = `Say "hi"`;
console.log(single);
console.log(double);
console.log(backtick);

Output

It works
It's easy
Say "hi"

Template literals

Backtick strings are template literals. They can embed any expression with ${...} and span multiple lines — the modern way to build strings:

Example 2Try it yourself
const user = "Ada";
const items = 3;
console.log(`Hello ${user}, you have ${items} items (${items * 2} after doubling).`);
const poem = `Roses are red,
JavaScript is fun.`;
console.log(poem);

Output

Hello Ada, you have 3 items (6 after doubling).
Roses are red,
JavaScript is fun.

Length and characters

Example 3Try it yourself
const word = "JavaScript";
console.log(word.length);
console.log(word[0], word.at(-1));
console.log("Tab:\tafter tab\nNew line");

Output

10
J t
Tab:	after tab
New line

Strings are immutable: methods like toUpperCase() return a new string and leave the original untouched.

Escape characters

Code Result
\' \" quote inside a string
\\ backslash
\n new line
\t tab

Test yourself

Which syntax embeds a variable name inside a template literal?

  1. "Hi {name}"
  2. `Hi ${name}`
  3. 'Hi $name'
  4. "Hi " + {name}
Show answer

Answer: `Hi ${name}`