Home › Learn › JavaScript › Getting started

Strings and template literals

Lesson 4 of 40. Join text and put values inside it with backticks.

Strings join with +, but the readable way to build text from values is a template literal: text between backticks where ${expression} is replaced by the value. ${first} ${last} gives the two values with a space between, and the expression can be anything, including calculations and function calls.

Strings have properties and methods: .length, .toUpperCase(), .toLowerCase(), and [0] for the first character (positions start at zero). Methods return a new string; the original never changes.

const word = "script";
console.log(`${word} has ${word.length} letters`);
console.log(word.toUpperCase(), word[0]);

Your task

Using first and last from the starter, make full (the two names with a space between, using a template literal), shout (the full name in upper case), and initials (the first character of each name joined, so AL). Print full.

Hint 1

const full = `${first} ${last}`;

Hint 2

first[0] + last[0]

Show the solution
const first = "Ada";
const last = "Lovelace";

const full = `${first} ${last}`;
const shout = full.toUpperCase();
const initials = first[0] + last[0];
console.log(full);

    Runs in an isolated web worker in this tab; nothing is sent anywhere.