Guide 02 / 4
JavaScript essentials
An annotated cheat sheet: what each element is for, how to write it and when to use it.
Choose and use the essential elements to complete this step of the blog.
01 · Programming basics
Practise on small values first before touching the DOM.
Loading a script with defer
Create app.js next to index.html. Add this tag in head: defer waits for the document to be parsed before running the script.
Example
<script src="app.js" defer></script>
trim, toLocaleLowerCase and includes
trim removes spaces at the ends. Converting to lowercase makes the comparison case-insensitive. includes looks for a substring.
Example
const titre = "Soupe de carottes".toLocaleLowerCase("fr");
const recherche = " SOUPE ".trim().toLocaleLowerCase("fr");
console.log(titre.includes(recherche)); // true
This version keeps accents: “cafe” does not automatically match “café”.
Number and numeric comparison
Convert the values read from the DOM before comparing durations. Two strings can be compared alphabetically, which is surprising for numbers.
Example
console.log(Number("10") <= Number("20")); // true
console.log("100" < "20"); // true, comparaison de chaînes
forEach and counter
forEach goes through all the cards. The counter starts at 0 on each filtering, then increases for each visible result.
Example
let visibles = 0;
cartes.forEach((carte) => {
if (!carte.hidden) { visibles += 1; }
});
Arrow function and template string
An arrow function is a compact notation, often used in listeners. Backticks delimit a string in which ${…} inserts an expression.
Example
const rapide = (duree) => duree <= 20;
const total = 3;
console.log(`${total} recettes`);
02 · Reading, reacting and displaying
Operations are performed on the elements already present in the page.
input, change and submit
input reacts while typing. change suits the duration menu. submit lets you intercept the form submission, including with Enter.
Example
recherche.addEventListener("input", filtrerRecettes);
dureeMax.addEventListener("change", filtrerRecettes);
formulaire.addEventListener("submit", (evenement) => {
evenement.preventDefault();
filtrerRecettes();
});
reset and setTimeout
The form emits reset before restoring its initial values. Deferring the filtering makes it possible to reread the values after this restoration.
Example
formulaire.addEventListener("reset", () => {
setTimeout(filtrerRecettes, 0);
});
0 does not promise instant execution: the function waits for a later turn of processing.
Combining two filters
A card must match the title AND the duration. The menu value 0 means “no limit”. An empty search matches all titles.
Example
const titreOK = titre.includes(terme);
const dureeOK = limite === 0 || duree <= limite;
carte.hidden = !(titreOK && dureeOK);
Debugging with the console and a breakpoint
Observe the values before changing the logic. A breakpoint pauses the program; inspect terme, limite, titreOK and dureeOK for one card.
Example
console.log({ terme, limite, titreOK, dureeOK });
To be inserted temporarily inside the loop over the cards, then removed.