JavaScript — The basics

Guide 02 / 4

JavaScript essentials

An annotated cheat sheet: what each element is for, how to write it and when to use it.

Expected outcome :

Choose and use the essential elements to complete this step of the blog.

Reading
0%

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.

<script src="app.js" defer></script>
Remember : Do not put the

trim, toLocaleLowerCase and includes

trim removes spaces at the ends. Converting to lowercase makes the comparison case-insensitive. includes looks for a substring.

const titre = "Soupe de carottes".toLocaleLowerCase("fr");
const recherche = "  SOUPE  ".trim().toLocaleLowerCase("fr");
console.log(titre.includes(recherche)); // true
Remember :

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.

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.

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.

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.

value, hidden and textContent

value reads a field. hidden hides a card using a boolean. textContent updates an element's text.

const terme = recherche.value;
carte.hidden = true;
compteur.textContent = "0 recette affichée.";
Remember :

The [hidden] CSS rule from the previous course must stay in place.

input, change and submit

input reacts while typing. change suits the duration menu. submit lets you intercept the form submission, including with Enter.

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.

formulaire.addEventListener("reset", () => {
  setTimeout(filtrerRecettes, 0);
});
Remember :

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.

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.

console.log({ terme, limite, titreOK, dureeOK });
Remember :

To be inserted temporarily inside the loop over the cards, then removed.