JavaScript fundamentals
- Variables: Declaration / assignment / scope
- Data types: String / Number / Boolean / Array / Object / Null / Undefined / Symbol / bigInt
- Operator: Arithmetic / Assignment / Comparison / Logic / Bitwise
- Conditional: If / Else / Switch
- Loop: For / While / Do While / For In / For Of
- Function: Declaration / Expression / Arrow / Callback
Variables
Declaration variables
let name;
Assignment variables
name = "ATC";
Declaration and assignment combined:
let name = "ATC";
Variable scope
The scope of a variable is the area of code in which it can be accessed.
In JavaScript, variables can be declared in 4 different ways, each with a different scope:
var
var is function-scoped, not block-scoped. A top-level var in a classic script may also create a global property, unlike top-level declarations in an ES module. var allows redeclaration; prefer let and const for explicit scope.
let and const
These declarations are block-scoped, meaning they are limited to the blocks in which they are defined.
If you declare a variable with let or const inside a for loop, for example, it will not be accessible outside of that loop.
A const binding cannot be reassigned, but an object referenced by it can still be mutated. let allows reassignment.
Data Types
One of the most fundamental aspects of programming is understanding data types. In JavaScript, each value belongs to a specific data type.
String
- Description: Sequences of characters used to represent text.
let phrase = "phrase totalement random";
Number
- Description: Represents both integers and floating point numbers.
let age = 25;
let prix = 19.99;
Boolean
- Description: Represents a true (
true) or false (false) value.
let estMajeur = true;
Array
- Description: Structures used to store multiple values in a single variable.
let fruits = ["pomme", "banane", "cerise"];
Object
- Description: Collections of properties, represented by a key and a value.
let voiture = {
marque: "Toyota",
modèle: "Corolla",
année: 2020,
};
Null
- Description: Represents the intentional absence of value.
let data = null;
Undefined
- Description: An uninitialized variable has the value
undefined.
let test;
console.log(test); // Affiche "undefined"
Symbol
- Description: A unique, immutable data type.
const sym1 = Symbol("description");
bigInt
- Description: Allows you to represent integers larger than 2^53 - 1.
const bigNumber = 1234567890123456789012345678901234567890n;
Operators
Operators perform actions on variables and values. They are the essential tools for manipulating data.
Arithmetic:
- Example:
+,-,*,/,%
Assignment:
- Example:
=,+=,-=,*=,/=
Comparison:
- Example:
==,===,!=,!==,>,<,>=,<=
Logic:
- Example:
&&,||,!
Bitwise:
- Example:
&,|,^,~,<<,>>,>>>
** Conditional Structures**
Conditional structures allow you to execute blocks of code based on certain conditions.
If
if (condition) {
// code à exécuter si la condition est vraie
}
Else
if (condition) {
// code si la condition est vraie
} else {
// code si la condition est fausse
}
Switch
switch (expression) {
case x:
// code à exécuter si expression === x
break;
case y:
// code à exécuter si expression === y
break;
default:
// code à exécuter si expression ne correspond à aucune case
}
Curls
Loops are used to repeatedly execute a block of code.
For
for (initialisation; condition; mise à jour) {
// code à répéter
}
While
while (condition) {
// code à répéter tant que la condition est vraie
}
Do While
do {
// code à répéter
} while (condition);
For In
for (let cle in objet) {
// code pour chaque propriété de l'objet
}
For Of
for (let element of iterable) {
// code pour chaque élément de l'itérable (e.g., array)
}
Functions
Functions are blocks of code designed to perform a specific task.
Statement
function nomDeLaFonction(parametres) {
// corps de la fonction
}
Expression
const maFonction = function (parametres) {
// corps de la fonction
};
Arrow
const maFonction = (parametres) => {
// corps de la fonction
};
Callback
- Description: A function passed as an argument to another function to be executed after the latter has finished.
function saluer(callback) {
console.log("Bonjour");
callback();
}
saluer(function () {
console.log("Au revoir");
});