Learn JS step by step
Variables
Declaration, assignment and scope of variables in JavaScript:
Variable declaration
- Use
var,letorconstto declare a variable.
Assigning variables
Use = to assign a value to a variable.
Variable scope
- Global variables: Declared outside all functions, accessible everywhere.
- Local variables: Declared inside a function, accessible only inside it.
- Variables with block scope (ES6+):
letorconstinside a code block delimited by curly braces{}(closure), limited to this block. - Hoisting: Variable declarations only
varare hoisted to the top of their scope during code execution, but not their initialization.
Recommendations:
- Preference for
letandconst: Useletfor reassignable variables,constfor variables whose value does not change. - Limit scope: Declare variables in the narrowest possible scope.
- Name variables explicitly: Use meaningful names to facilitate understanding.
- Initialize the variables: Assign an initial value upon declaration.
Data type
FlashCard
| # | Data type | Description | Examples |
|---|---|---|---|
| 1. | Number | Represents numeric values | 42, 3.14, -10 |
| 2. | String | Represents a sequence of characters | 'Hello', "JavaScript" |
| 3. | Boolean | Represents a logical value (true or false) | true, false |
| 4. | Table | Ordered collection of elements | [1, 2, 3], ['a', 'b', 'c'] |
| 5. | Object | Entity with properties and methods | name: 'John', age: 25 |
| 6. | Undefined | Indicates that a variable has no assigned value | undefined |
| 7. | Null | Indicates intentional absence of value | null |
| 8. | Symbol | Unique identifiers introduced in ES6 | Symbol('description') |
| 9. | Date Object | Represents a specific date and time | new Date() |
| 10. | RegExp object | Used for regular expressions | /pattern/, new RegExp('pattern') |
| 11. | Function | Reusable code block called with name and parameters | function addition(a, b) return a + b; |
Operators
| # | Operator | Description |
|---|---|---|
| 1. | Arithmetic operators | +, -, *, /, % |
| 2. | Comparison operators |
| 3. | Logical operators | &&, ||, ! | | 4. | Assignment operators | =, +=, -=, *=, /=, %= | | 5. | Increment operators | ++, -- | | 6. | Concatenation operator | + | | 7. | Ternary operator | condition ? expression1: expression2 | | 8. | Bitwise operators | | | 9. | String concatenation operator | += | | 10. | Membership operators | in, instanceof | | 11. | Selection operator | . | | 12. | Type Test Operators | typeof, instanceof | | 13. | Shift operators | | | 14. | Delete operator | delete | | 15. | Scope resolution operator | :: | | 16. | Grouping operators | (), [], |
Control structure
| # | Control structure | Description |
|---|---|---|
| 1. | Conditional structure if/else | Allows you to execute a block of code based on a condition. |
| 2. | Switch conditional structure | Allows you to select a block of code to execute among several cases. |
| 3. | for loop | Executes a block of code a certain number of times, based on a check condition. |
| 4. | while loop | Executes a block of code as long as a given condition is true. |
| 5. | do...while loop | Executes a block of code at least once, then continues until a given condition is true. |
| 6. | Loop for...in | Iterates through the enumerable properties of an object. |
| 7. | for...of loop | Iterates through the elements of an iterable structure (array, string, etc.). |
| 8. | Ternary conditional structure | Allows you to perform a condition in a single line. |
| 9. | Break instructions | Ends execution of a loop or conditional statement. |
| 10. | Continued instruction | Moves to the next iteration of a loop. |
Functions
| # | Function | Description | Courses and examples |
|---|
| 1. | Declarative function | Declares a function with the keyword function. | Course: function nomFonction() { // Code }
Example: function afficherMessage() { console.log("Bonjour !"); } |
| 2. | Function with parameters | Allows you to pass values to a function using parameters. | Course: function nomFonction(param1, param2) { // Code } Example: function additionner(a, b) { return a + b; } |
| 3. | Function with return value | Returns a value calculated by the function with the return keyword. | Course: function nomFonction() { return valeur; } Example: function carre(nombre) { return nombre * nombre; } |
| 4. | Anonymous function | Unnamed function, often used as an argument to another function. | Course: (function() { // Code })(); Example: (function() { console.log("Fonction anonyme exécutée !"); })(); |
| 5. | Arrow function | Simplified syntax for declaring a function. | Course: () => { // Code } Example: const multiplier = (a, b) => a * b; |
| 6. | Recursive function | Calls itself to perform repetitive operations. | Course: function nomFonction() { nomFonction(); } Example: function compteur(numero) { if (numero > 0) { console.log(numero); compteur(numero - 1); } } |
| 7. | Callback function | Function passed as an argument to another function to be called later. | Course: function fonctionPrincipale(callback) { // Code ; callback(); } Example: function afficherMessage(callback) { console.log("Bonjour !"); callback(); } afficherMessage(() => console.log("Fin du programme")); |
| 8. | Self-executing function (IIFE) | Runs immediately after setting. | Course: (function() { // Code })(); Example: (function() { console.log("Fonction auto-exécutante !"); })(); |
Declarative function
- A declarative function is defined using the keyword
function. - It can have a name to be reused later.
function afficherMessage() {
console.log("Bonjour !");
}
afficherMessage(); // Appel de la fonction