|
| 1 | +// Loops & Functions |
| 2 | +// Loops and functions are essential concepts in JavaScript programming that help to create efficient and reusable code. |
| 3 | + |
| 4 | + |
| 5 | +// Types Of Loops |
| 6 | +//For Loop -> Loop a Block Of Code No Of Time |
| 7 | +//For In Loop -> Loop Through The Key Of An Object |
| 8 | +//For Of Loop -> Loop Through The Value Of An Object |
| 9 | +//While Loop -> Loop Of Block Based On Specific Condition |
| 10 | +// Do...While Loop -> While Loop Variant Which Run a At Least Once |
| 11 | + |
| 12 | + |
| 13 | +// For Loop |
| 14 | +// Syntax |
| 15 | +// for(Statement1;Statement2;Statement3){ |
| 16 | +// // Code To Be Executed |
| 17 | +// } |
| 18 | +// Statement1 -> Executed One Time |
| 19 | +// Statement2 -> Condition Based -> Based On This Loop Body Will Be Executed |
| 20 | +// Statement3 -> Executed Every Time The Loop Body Is Executed |
| 21 | + |
| 22 | + |
| 23 | +// Example |
| 24 | +for (let i = 0; i < 5; i++) { |
| 25 | + console.log(i); // prints numbers from 0 to 4 |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +// For In Loop |
| 30 | +//Loop Through The Key Of An Object |
| 31 | +// For In Loop Works With Array Also |
| 32 | + |
| 33 | +// Example |
| 34 | +const person = { |
| 35 | + name: 'John', |
| 36 | + age: 30, |
| 37 | + occupation: 'Developer' |
| 38 | + // key:'value' |
| 39 | +}; |
| 40 | + |
| 41 | +for (let key in person) { //(let a in person) -> console.log(a); |
| 42 | + console.log(key); // prints "name", "age", "occupation" |
| 43 | + console.log(person[key]); // prints the corresponding values "John", 30, "Developer" |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +// For Of Loop |
| 48 | +// Loop Through The Value Of An Object |
| 49 | +// Object Must Be Iterable |
| 50 | + |
| 51 | +// Example |
| 52 | +const fruits = ['apple', 'banana', 'orange']; |
| 53 | + |
| 54 | +for (let fruit of fruits) { |
| 55 | + console.log(fruit); // prints "apple", "banana", "orange" |
| 56 | +} |
| 57 | + |
| 58 | +const message = 'Hello'; |
| 59 | + |
| 60 | +for (let char of message) { |
| 61 | + console.log(char); // prints "H", "e", "l", "l", "o" |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +// While Loop |
| 66 | +// If Condition Never False -> Loop Will Never End -> Crush JS Run Time |
| 67 | +// Also Called In Infinite Loop -> Don't Try This Circus |
| 68 | + |
| 69 | +// Syntax |
| 70 | +// while(condition){ |
| 71 | +// // Code To Be Executed |
| 72 | +// } |
| 73 | + |
| 74 | +// Example |
| 75 | +let i = 0; |
| 76 | +while (i < 5) { |
| 77 | + console.log(i); // prints numbers from 0 to 4 |
| 78 | + i++; |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +// ALT + SHIFT + DOWN ARROW KEY -> Replicate Selected Code |
| 83 | + |
| 84 | + |
| 85 | +// Do.....While Loop |
| 86 | +// Do...While Loop -> While Loop Variant Which Run a At Least Once |
| 87 | +// Syntax |
| 88 | + |
| 89 | +// do { |
| 90 | +// // Code To Be Executed |
| 91 | +// } while(Condition) |
| 92 | + |
| 93 | +// Example |
| 94 | +let i = 0; |
| 95 | +do { |
| 96 | + console.log(i); // prints numbers from 0 to 4 -> Executed At Least Once |
| 97 | + i++; |
| 98 | +} while (i < 5); |
| 99 | + |
| 100 | +let i = 6; |
| 101 | +do { |
| 102 | + console.log(i); // prints numbers from 0 to 4 -> Executed At Least Once |
| 103 | + i++; |
| 104 | +} while (i < 5); |
| 105 | + |
| 106 | +// Output -> 6 |
| 107 | + |
| 108 | + |
| 109 | +// Functions |
| 110 | +// Functions are reusable blocks of code that perform a specific task. They help in organizing code, improving reusability, and reducing redundancy. A function can accept parameters (inputs) and return a value. |
| 111 | + |
| 112 | + // function myfun(parameter1, parameter2){ |
| 113 | + // //Code -> Parameter Behave As Local Variables |
| 114 | + // } |
| 115 | + |
| 116 | +// myfun(6,7); -> Function Invocation |
| 117 | +// A. Function declaration: |
| 118 | +// B function declaration defines a named function that can be called later in the code. |
| 119 | + |
| 120 | +// Example |
| 121 | +function greet(name) { |
| 122 | + console.log(`Hello, ${name}!`); |
| 123 | +} |
| 124 | + |
| 125 | +greet("John"); // prints "Hello, John!" |
| 126 | +greet("Sarah"); // prints "Hello, Sarah!" |
| 127 | + |
| 128 | +// Function expression: |
| 129 | +// A function expression assigns a function to a variable. It can be anonymous or named. |
| 130 | + |
| 131 | +const greet = function (name) { |
| 132 | + console.log(`Hello, ${name}!`); |
| 133 | +}; |
| 134 | + |
| 135 | +greet("John"); // prints "Hello, John!" |
| 136 | +greet("Sarah"); // prints "Hello, Sarah!" |
| 137 | + |
| 138 | +// Arrow function: |
| 139 | +// Arrow functions provide a concise syntax for writing functions. They are anonymous and lexically bind the this value. |
| 140 | + |
| 141 | +const greet = (name) => { |
| 142 | + console.log(`Hello, ${name}!`); |
| 143 | +}; |
| 144 | + |
| 145 | +greet("John"); // prints "Hello, John!" |
| 146 | +greet("Sarah"); // prints "Hello, Sarah!" |
| 147 | + |
| 148 | +// Returning a value: |
| 149 | +// Functions can return a value using the return statement. The returned value can be stored in a variable or used directly. |
| 150 | + |
| 151 | +function add(a, b) { |
| 152 | + return a + b; |
| 153 | +} |
| 154 | + |
| 155 | +const result = add(3, 4); |
| 156 | +console.log(result); // prints 7 |
| 157 | + |
| 158 | +// Default parameters: |
| 159 | +// Default parameters allow you to assign default values to function parameters if no argument is passed. |
| 160 | + |
| 161 | + |
| 162 | +function multiply(a, b = 1) { |
| 163 | + return a * b; |
| 164 | +} |
| 165 | + |
| 166 | +console.log(multiply(5)); // prints 5 |
| 167 | +console.log(multiply(5, 2)); // prints 10 |
0 commit comments