diff --git a/JsLoops/do-while.md b/JsLoops/do-while.md new file mode 100644 index 00000000..9c7f3194 --- /dev/null +++ b/JsLoops/do-while.md @@ -0,0 +1,24 @@ +# JavaScript Do-While Loop + +The `do-while` loop in JavaScript is a control flow statement that repeatedly executes a block of code as long as a specified condition is true. The key difference between a `do-while` loop and other loop structures is that it always executes the block of code at least once before checking the condition. + +## Syntax + +```javascript +do { + // code block to be executed +} while (condition); +``` +## Use Cases +1) When you need to execute a block of code at least once, regardless of the initial condition.
+2) When the loop's condition depends on the result of the code block's execution. + +### Example +```javascript +let count = 0; + +do { + console.log(`Count: ${count}`); + count++; +} while (count < 5); +``` \ No newline at end of file diff --git a/JsLoops/for-each.md b/JsLoops/for-each.md new file mode 100644 index 00000000..27c04db7 --- /dev/null +++ b/JsLoops/for-each.md @@ -0,0 +1,34 @@ +# JavaScript forEach + +## Description +The `forEach` method is used to iterate over elements in an array and execute a provided function once for each array element. + +## Syntax +```javascript +array.forEach(callback(currentValue [, index [, array]]) { + // Your code here +}, thisArg); +``` +### Parameters +```callback:``` Function to execute for each element.
+```currentValue:``` The current element being processed in the array.
+```index (optional):``` The index of the current element being processed in the array.
+```array (optional):``` The array forEach was called upon.
+```thisArg (optional):``` Object to use as this when executing the callback.
+ +## Example +```javascript +const numbers = [1, 2, 3, 4, 5]; + +numbers.forEach(function (num, index) { + console.log(`Element at index ${index}: ${num}`); +}); +``` +Output : +``` +Element at index 0: 1 +Element at index 1: 2 +Element at index 2: 3 +Element at index 3: 4 +Element at index 4: 5 +``` \ No newline at end of file diff --git a/JsLoops/for-in-loop.md b/JsLoops/for-in-loop.md new file mode 100644 index 00000000..2b05fcf6 --- /dev/null +++ b/JsLoops/for-in-loop.md @@ -0,0 +1,32 @@ +# JavaScript For...In Loop + +The `for...in` loop in JavaScript is used to iterate over the properties of an object. It is particularly useful when you want to loop through the keys (property names) of an object. + +## Syntax + +```javascript +for (variable in object) { + // code to be executed +} +``` +```variable:``` A variable that will be assigned the property name on each iteration.
+```object:``` The object whose enumerable properties are iterated. +## Note +The for...in loop iterates over enumerable properties, including inherited ones.
+It is recommended to use caution when using for...in with arrays, as it may iterate over array methods and not just array elements. + +### Example +```javascript +// Sample object +const car = { + make: 'Toyota', + model: 'Camry', + year: 2022 +}; + +// Using for...in loop to iterate over object properties +for (let key in car) { + console.log(key + ': ' + car[key]); +} + +``` \ No newline at end of file diff --git a/JsLoops/for-loop.md b/JsLoops/for-loop.md new file mode 100644 index 00000000..5efa8494 --- /dev/null +++ b/JsLoops/for-loop.md @@ -0,0 +1,59 @@ +# JavaScript For Loop + +## Description + +The JavaScript `for` loop is a control flow statement that allows you to execute a block of code repeatedly. It consists of three optional expressions: initialization, condition, and iteration. The loop continues to execute as long as the condition is true. Here's the basic syntax: + +```javascript +for (initialization; condition; iteration) { + // code to be executed +} +``` +## Features +Initialization: Declare and initialize a loop variable.
+Condition: Specify the condition to continue the loop.
+Iteration: Define how the loop variable changes after each iteration.
+## Example + ### Simple Numeric Loop + ```javascript +for (let i = 0; i < 5; i++) { + console.log(i); +} + +``` +This will output: +``` +0 +1 +2 +3 +4 +``` +### Looping Through an Array +```javascript +const fruits = ['apple', 'orange', 'banana']; + +for (let i = 0; i < fruits.length; i++) { + console.log(fruits[i]); +} +``` +This will output: +``` +apple +orange +banana +``` +### Backward Loop +```javascript +for (let i = 5; i > 0; i--) { + console.log(i); +} +``` +This will output: +``` +5 +4 +3 +2 +1 +``` \ No newline at end of file diff --git a/JsLoops/for-of-loop.md b/JsLoops/for-of-loop.md new file mode 100644 index 00000000..81e00a47 --- /dev/null +++ b/JsLoops/for-of-loop.md @@ -0,0 +1,25 @@ +# JavaScript for...of Loop README + +## Description + +The `for...of` loop in JavaScript is used to iterate over iterable objects, such as arrays, strings, maps, sets, and more. It provides a concise and readable syntax for iterating through the elements of a collection. + +## Syntax + +```javascript +for (variable of iterable) { + // code to be executed +} +``` +``` variable: ``` A variable to represent the current element of the iterable in each iteration.
+``` iterable: ``` The object or array-like structure to be iterated over. +### Example +#### Iterating over an Array +```javascript +const numbers = [1, 2, 3, 4, 5]; + +for (const number of numbers) { + console.log(number); +} + +``` diff --git a/JsLoops/while-loop.md b/JsLoops/while-loop.md new file mode 100644 index 00000000..1e6704f8 --- /dev/null +++ b/JsLoops/while-loop.md @@ -0,0 +1,31 @@ +# JavaScript While Loop + +The JavaScript `while` loop is used to repeatedly execute a block of code as long as a specified condition is true. It is one of the fundamental control flow structures in JavaScript.
+The while loop provides a flexible way to repeat a block of code based on a condition. + +## Syntax + +```javascript +while (condition) { + // code to be executed +} +``` +### Example +```javascript +let count = 0; + +while (count < 5) { + console.log(`Current count: ${count}`); + count++; +} + +console.log("Loop finished!"); +``` +## Infinite Loop +when using while loops to avoid creating infinite loops. An infinite loop occurs when the condition always evaluates to true, leading to continuous execution of the loop. +```javascript +// Caution: Infinite Loop +while (true) { + console.log("This is an infinite loop!"); +} +``` \ No newline at end of file