Fiche de Revision JavaScript Complete
1. Les bases
- let, const, var dclarer des variables
- typeof, parseInt, parseFloat
- alert, prompt, console.log
- Oprateurs : +, -, *, /, %, ==, ===, !=, !==, &&, ||, !
Exemple :
let nom = "Janai";
console.log("Bonjour " + nom);
2. Les boucles
- for, while, do...while
Exemple :
for (let i = 0; i < 5; i++) {
console.log("Tour : " + i);
}
3. Les fonctions
Fonctions classiques :
function addition(a, b) {
return a + b;
}
Fonctions flechees :
const addition = (a, b) => a + b;
4. Les tableaux (Array)
let fruits = ["pomme", "banane", "orange"];
console.log(fruits[1]); // banane
push(), pop(), shift(), unshift(), splice(), forEach(), map(), filter()
5. Les objets
let personne = {
nom: "Janai",
age: 25,
saluer() {
return "Bonjour " + this.nom;
}
};
6. JSON
let jsonStr = '{"nom":"Janai","age":25}';
let obj = JSON.parse(jsonStr);
console.log(obj.nom); // Janai
7. DOM
document.getElementById("titre").textContent = "Nouveau titre";
let div = document.createElement("div");
div.textContent = "Hello";
document.body.appendChild(div);
8. Evenements
document.getElementById("btn").addEventListener("click", () => {
alert("Clic !");
});
9. Promesses & async
fetch("https://api.exemple.com/data")
.then(res => res.json())
.then(data => console.log(data))
.catch(err => console.error(err));
async function charger() {
try {
let res = await fetch("https://api.exemple.com/data");
let data = await res.json();
console.log(data);
} catch (e) {
console.error(e);
}
}
10. Modules
// util.js
export function direBonjour(nom) {
return "Bonjour " + nom;
}
// main.js
import { direBonjour } from './util.js';
console.log(direBonjour("Janai"));