×

javascript · level 2 · unit 1 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Put a new value in .textContent and the page changes.

.textContent works both ways. Read it to see an element's words. Put a new string into it with = and the old words vanish, replaced on the page at once. It is the same = you use for any variable.

Any value works. A template literal can build the words from variables, and a number turns into text by itself: points.textContent = 15; shows 15.

If your selector matches nothing, querySelector hands back , which means nothing is here. Asking null for its .textContent crashes with a TypeError, so check the spelling and the #.

<h1 id="greeting">Hello, stranger!</h1>
<p id="points">0</p>
const player = "Sam";
const greeting = document.querySelector("#greeting");
const points = document.querySelector("#points");
greeting.textContent = `Welcome back, ${player}!`;
points.textContent = 3 * 5;

Press run it to bring the page to life.

console

nothing logged yet

Your HTML stays the same. The code changes the live page in the browser.