Stop at Nothing: ?.
"Open the box, take out the bag, and give me the key in it." If there is no box, you stop at the first step and say "there is no box". You don't try to open a bag that isn't there. JavaScript has a way to stop like that, in the middle of a line.
A chain of dots
Some lines go through several steps with dots. This is called a chain:
const name = document.querySelector("#hero").textContent.trim();
Find the element, take its text, and trim it. If there is no #hero, the first step gives null, and the second step stops everything:
TypeError: Cannot read properties of null (reading 'textContent')
?.: stop at nothing
?. works like a dot, but first it checks what is on its left. If that's null or undefined, it stops there, and the whole chain gives undefined, with no error:
const name = document.querySelector("#hero")?.textContent.trim();
console.log(name);
On a page without #hero:
undefined
On a page with it, the chain goes on as usual, and name is the hero's text.
Only put ?. where something might be missing. Here, only the element might be missing. If it's there, it has a textContent, so the next dots stay plain dots.
What comes back
After a ?. chain, you have either the value or undefined. An if or a ternary decides what to do with each:
const name = document.querySelector("#hero")?.textContent;
console.log(name ? `Our hero: ${name}` : "No hero today");
That's shorter than an if that checks for null first, and it reads in the order things happen.
Break it on purpose
Put the ?. one step too late: document.querySelector("#hero").textContent?.trim(). On a page without #hero, the error comes back. The plain dot after querySelector(...) already failed, before ?. could check anything. The ?. goes right after the thing that might be missing.
Mission: The motion that's chosen
What you will see: under the motion buttons, the page says:
Motion: full
Under the motion buttons, the page should say which motion is chosen, from the button pressed in the HTML.
- Under the motion buttons, add a paragraph with the id
current-motion. - When the page opens, it says
Motion:, then the motion of the button pressed in the HTML: the part of its id aftermotion-, sofullorless. - The
settingsobject keeps it in a property,motion. - When no motion button is pressed in the HTML, it says
Motion: not chosen, and the page shows no error.
The ?. goes right after the querySelector(...) that might find nothing.