Posts

Showing posts with the label JavaScript Loop

'for in' Loop

Introduction to JavaScript 'for in' Loop The `for in` loop is a type of loop in JavaScript that is used to iterate over the properties of an object. It is often used when you need to loop through the keys or properties of an object and perform some action on each one. Unlike other loops like `for` and `while`, the `for in` loop is specifically designed to work with objects. Syntax The syntax for the `for in` loop is as follows: for   ( variable  in  object )   {    // code to be executed } The `variable` represents a temporary variable that will hold the key of each property in the object. The `object` is the object that you want to iterate over. Example Let's take a look at an example to understand how the `for in` loop works: const  person  =   {   name :   'John' ,   age :   30 ,   gender :   'male' } ;   for ...

'for of' Loop

Introduction to 'for of' Loop The `for of` loop is a new feature introduced in ECMAScript 6 (ES6) for iterating over iterable objects in JavaScript. It provides an easy and concise way to iterate through the values of an array or other iterable objects. The syntax of the `for of` loop is as follows: for   ( let variable of iterable )   {    // code to be executed } The `variable` represents the current value being iterated, and `iterable` represents the array or iterable object to be looped over. Inside the loop, you can perform any desired operations using the current value. Let's look at an example to see how the `for of` loop works: let fruits  =   [ 'apple' ,   'banana' ,   'cherry' ] ;   for   ( let fruit of fruits )   {   console. log ( fruit ) ; } In this example, the `for of` loop iterates through each value of the `fruits` array, and the current value is assig...

'do while' Loop

What is a 'do while' Loop? In JavaScript, the 'do while' loop is similar to the 'while' loop but with one key difference. The 'do while' loop executes a block of code once, and then continues to execute the code as long as a specified condition is true. The key difference is that the code block is always executed at least once, regardless of whether the condition is true or false. There are mainly two types of loops. Entry Controlled loops: In this type of loop, the test condition is tested before entering the loop body.  For Loop  and  While Loops  are entry-controlled loops. Exit Controlled Loops: In this type of loop the test condition is tested or evaluated at the end of the loop body. Therefore, the loop body will execute at least once, irrespective of whether the test condition is true or false. the  do-while loop  is exit controlled loop. The syntax for a 'do while' loop in JavaScript is as follows: do   {     // code ...

while Loop

Introduction to 'while' Loop In JavaScript, a `while` loop is a type of loop that executes a block of code repeatedly as long as a specified condition is true. It is called a "while" loop because it repeats the code until the condition becomes false. The `while` loop is useful when you want to execute a block of code an unknown number of times. Syntax The syntax of a `while` loop is as follows: while  ( condition )   {    // code to be executed } The condition is evaluated before each iteration. If the condition is true, the code inside the loop is executed. Once the condition becomes false, the loop stops executing and the program continues with the next line of code after the loop. Example Let's say we want to print numbers from 1 to 5 using a `while` loop: let count  =   1 ;   // initialization   while  ( count  <=   5 )   {   // condition   console. log ( count ) ;  ...