JavaScript is the programming language of the Web: it runs in every browser to make pages interactive, and on the server with Node.js. In this course, our exercises run with Node, without a web page: we focus on the language itself.
Declaring variables
const prixUnitaire = 19.9; // const: cannot be reassigned
let quantite = 2; // let: can change
quantite = 3; // OK
// prixUnitaire = 25; // TypeError: assignment to a constant
console.log(prixUnitaire * quantite); // prints 59.7 (approximately, see below)
Simple rule: use const by default, and let only if you must reassign the variable. The old var keyword has tricky behaviors (function scope, declaration “hoisting”): it is no longer used in modern code.
Names follow the camelCase convention (prixTotal, estConnecte) and should be meaningful.
Primitive types
const nom = "Léa"; // string
const age = 31; // number (integers and decimals)
const estAbonne = true; // boolean
let commentaire; // undefined: declared without a value
const coupon = null; // null: deliberate absence of a value
const grandNombre = 9007199254740993n; // bigint: arbitrarily large integers
console.log(typeof nom); // "string"
console.log(typeof age); // "number"
console.log(typeof null); // "object": a historical quirk to know about
There is also the symbol type, which is rarer. Everything else (arrays, objects, functions) is of the object type.
Operators and strings
const total = 10 + 5 * 2; // 20: multiplication takes precedence
const reste = 17 % 5; // 2: remainder of the division (modulo)
const carre = 3 ** 2; // 9: exponentiation
let compteur = 0;
compteur += 1; // compteur = compteur + 1
compteur++; // increments by 1
// Template literals (backticks): insert expressions with ${...}
const message = `Hello ${nom}, you are ${age} years old.`;
const ligne = `Total : ${(19.9 * 3).toFixed(2)} €`; // "Total : 59.70 €"
Strings have many methods: nom.length, nom.toUpperCase(), texte.trim(), texte.includes("@"), texte.split(","), texte.padStart(2, "0").
Comparing: always with ===
5 === 5 // true
5 === "5" // false: different types
5 == "5" // true: == converts types before comparing (trap)
0 == false // true (!)
null == undefined // true
=== and !== compare without conversion: use them systematically. Other operators: <, >, <=, >=, and the logical operators && (and), || (or), ! (not).
Converting explicitly
Data coming from a form or a URL are strings. You need to convert them:
const saisie = "42";
const n = Number(saisie); // 42
"3" + 4 // "34": + concatenates if a string is present
Number("3") + 4 // 7
Number("douze") // NaN (Not a Number)
Number.isNaN(Number("douze")) // true
Number.parseFloat("12.5 kg") // 12.5: reads the start of the string
String(42) // "42"
The decimal trap
0.1 + 0.2 // 0.30000000000000004
Numbers are stored in binary floating point: some decimals cannot be represented exactly. For money, calculate in whole cents (1990 rather than 19.90) and format only for display.
Exercises
Solutions and tests stay on the server. In the student space, your code is run and checked, and the tutor gives hints before the solution.
Welcome message
Declare a constant
prenomequal to"Inès"and a constantnbArticlesequal to3. Then create the constantmessagewith a template literal to get exactly:Bonjour Inès, votre panier contient 3 articles.// déclarez prenom, nbArticles puis messageConvert and calculate
The values
saisiePrixandsaisieQtecome from a form (they are strings). CalculatetotalCentimes, the total in whole cents (price x quantity x 100, rounded withMath.round), thenaffichagein the format"Total : 59.70 €"(two decimals withtoFixed(2)).const saisiePrix = "19.90"; const saisieQte = "3"; // vos calculsStrict comparison
Write the function
memeValeurEtType(a, b)that returnstrueifaandbare strictly equal (same value and same type),falseotherwise.function memeValeurEtType(a, b) { // votre code }
End-of-lesson quiz
What is “3” + 4 in JavaScript?
- 7
- "34"
- NaN
Which keyword should you use by default to declare a variable?
- var
- let
- const
What does typeof null return?
- "null"
- "object"
- "undefined"
The rest of the course
- Conditions and Loops20 min
- Functions20 min
- Arrays and Their Methods20 min
- Objects and JSON20 min
- Handling Errors and Organizing Code into Modules15 min