Strings and Template Literals

Build text with template literals and use the most common string methods.

JavaScript · beginner · 5 min · 8 steps · free

What you'll learn

Strings.js

const first = 'Ada';
const last = 'Lovelace';
const full = `${first} ${last}`;
const upper = full.toUpperCase();
const initials = first[0] + last[0];
const hasLove = full.includes('Love');
const parts = full.split(' ');
console.log(full.length, upper, initials, hasLove, parts);

Step by step

  1. first is created with the value "Ada".
  2. last is created with the value "Lovelace".
  3. A template literal puts the values of first and last into the text.
  4. toUpperCase returns a new string. full itself does not change.
  5. first[0] is the first character, "A". Indexes start at 0.
  6. hasLove is created with the value true.
  7. split(" ") cuts the text at each space and returns an array of the pieces.
  8. It prints 12 ADA LOVELACE AL true [ 'Ada', 'Lovelace' ].

Common mistakes

Best practices

Key takeaways

Check yourself

"abc".length?

Answer: 3

After s.toUpperCase(), s is?

Answer: Unchanged. Strings are immutable.

·

More in The Basics