Values & variables
Keep a value. Use it somewhere useful.
Names for values
A variable gives a value a name. Use const when the binding will not be reassigned. Use let when you intend to assign a new value later. Strings represent text; numbers support arithmetic.
Build a message
A template literal uses backticks and inserts expressions with ${...}. It is useful for combining text and values without a chain of plus signs.
Connect it to the page
document.querySelector finds the first matching element. Assigning textContent replaces its text. Unlike innerHTML, textContent does not interpret the assigned string as markup.
Try the example
<h1 id="greeting"></h1>
<p id="total"></p>
<script>
const name = "curious person";
const lessons = 15;
const minutes = 5;
document.querySelector("#greeting").textContent = `Hello, ${name}.`;
document.querySelector("#total").textContent = `${lessons * minutes} minutes of small beginnings.`;
</script>Enable JavaScript to edit and run the example in the playground.
Your turn
Change name to your name, then change minutes and run again.
Check your understanding
Which declaration allows reassignment?
- const
- let
- Neither
Show answer and explanation
let. let permits assigning a new value. const prevents reassignment of the binding.