Skip to main content

Learn JS step by step

Variables

Declaration, assignment and scope of variables in JavaScript:

Variable declaration

  • Use var, let or const to 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+): let or const inside a code block delimited by curly braces {} (closure), limited to this block.
  • Hoisting: Variable declarations only var are hoisted to the top of their scope during code execution, but not their initialization.

Recommendations:

  • Preference for let and const: Use let for reassignable variables, const for 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 typeDescriptionExamples
1.NumberRepresents numeric values ​​42, 3.14, -10
2.StringRepresents a sequence of characters'Hello', "JavaScript"
3.BooleanRepresents a logical value (true or false)true, false
4.TableOrdered collection of elements[1, 2, 3], ['a', 'b', 'c']
5.ObjectEntity with properties and methodsname: 'John', age: 25
6.UndefinedIndicates that a variable has no assigned valueundefined
7.NullIndicates intentional absence of valuenull
8.SymbolUnique identifiers introduced in ES6Symbol('description')
9.Date ObjectRepresents a specific date and timenew Date()
10.RegExp objectUsed for regular expressions/pattern/, new RegExp('pattern')
11.FunctionReusable code block called with name and parametersfunction addition(a, b) return a + b;

Operators

#OperatorDescription
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 structureDescription
1.Conditional structure if/elseAllows you to execute a block of code based on a condition.
2.Switch conditional structureAllows you to select a block of code to execute among several cases.
3.for loopExecutes a block of code a certain number of times, based on a check condition.
4.while loopExecutes a block of code as long as a given condition is true.
5.do...while loopExecutes a block of code at least once, then continues until a given condition is true.
6.Loop for...inIterates through the enumerable properties of an object.
7.for...of loopIterates through the elements of an iterable structure (array, string, etc.).
8.Ternary conditional structureAllows you to perform a condition in a single line.
9.Break instructionsEnds execution of a loop or conditional statement.
10.Continued instructionMoves to the next iteration of a loop.

Functions

#FunctionDescriptionCourses 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