lecture 2 of 150 completed

Strings and template literals

Text read from a page is always a string. You also build selectors, messages and unique test data from strings.

What you'll learn

  • Why text from a page needs converting before arithmetic
  • How to build a string with a template literal
  • How to make test data that is different on every run

The Shelf basket shows 3 books. A test adds one more and expects the basket to show 4. The test works out its expected number like this:

const basketText = "3";
const total = basketText + 1;
console.log(total);
console.log(typeof total);
31
string

The expected number is 31, so the test fails on a page that is correct.

A string is text. You write it inside quotes. "3" is a string that contains the character 3. It is not the number 3.

With two numbers, + adds. When either side is a string, + joins the two into a longer string. typeof tells you the kind of a value, and here it confirms that total is text.

Every testing tool gives you page text as a string. The page may show a price, a count or a date. Your test still receives text.

Convert before you calculate

Number() turns a string into a number.

const basketText = "3";
const total = Number(basketText) + 1;
console.log(total);
console.log(Number("3 books"));
4
NaN

NaN means "not a number". You get it when the text cannot be converted. "3 books" has letters in it, so the conversion fails. NaN === 4 is false, and so is NaN === NaN. A check for an exact number fails, which is the result you want from bad input.

Page text often has spaces or a line break around it. trim() removes them from both ends.

const heading = "  Dune \n";
console.log(heading === "Dune");
console.log(heading.trim() === "Dune");
false
true

Build strings with a template literal

Joining with + gets hard to read when there are several parts. A template literal is a string written inside backticks. Inside it, ${...} puts a value into the text.

const title = "Dune";
const copies = 2;
console.log("Book " + title + " has " + copies + " copies");
console.log(`Book ${title} has ${copies} copies`);
Book Dune has 2 copies
Book Dune has 2 copies

Both lines print the same text. The second one reads like the sentence it produces.

Tests use template literals in two places all the time.

const row = 3;
console.log(`[data-row="${row}"]`);

const email = `reader-${Date.now()}@shelf.test`;
console.log(email.endsWith("@shelf.test"));
[data-row="3"]
true

The first builds a selector from a variable. The second builds an email address for a new reader. Date.now() gives the current time as a count of milliseconds, so the address is different on each run.

That matters on the job. A test that signs up reader@shelf.test passes once. On the second run the address is taken, the sign-up fails, and the team spends time on a failure that is not a bug in the app.

Remember this

Text from a page is a string, even when it shows a number. Convert it with Number() before you calculate. Build messages, selectors and test data with template literals.

Reference: Template literals on MDN.

now prove it

Reading is the setup. Solve this exercise in the editor. We run your code on each case and compare what it prints with the expected output.