Basics of Loops in JavaScript š
A Thread ā
A Thread ā
ā” Loops
Loops in any programming language are used to run a piece of code repeatedly.
Using loops, you can run the same code with different values repeatedly without writing the instructions again and again.
Loops in any programming language are used to run a piece of code repeatedly.
Using loops, you can run the same code with different values repeatedly without writing the instructions again and again.
Types of loops:
ā§ for loop
ā§ while loop
ā§ do while loop
ā§ for...of loop
ā§ for...in loop
ā§ for loop
ā§ while loop
ā§ do while loop
ā§ for...of loop
ā§ for...in loop
ā§ for loop
For loop, it works best when you know how many iterations will be required in advance.
Syntax:
for (initialization; condition; increment/decrement) {
// Code to be executed in each iteration
}
For loop, it works best when you know how many iterations will be required in advance.
Syntax:
for (initialization; condition; increment/decrement) {
// Code to be executed in each iteration
}
There are three parts of for-loop:
1. initialization to declare or initialize a variable.
2. condition to determine whether the loop will keep executing; if the condition is false, the loop will be terminated.
1. initialization to declare or initialize a variable.
2. condition to determine whether the loop will keep executing; if the condition is false, the loop will be terminated.
ā§ while loop
In a `while` loop, you execute a block of code repeatedly as long as a certain condition is true.
The `while` loop works well when the number of iterations is unknown.
In a `while` loop, you execute a block of code repeatedly as long as a certain condition is true.
The `while` loop works well when the number of iterations is unknown.
for-loop vs. while-loop
One key difference between `for-loop` and `while-loop` is that `while-loop` is used when you do not know how many iterations are required in advance.
On the other hand, `for-loop` works best when you know how many iterations will be required in advance.
One key difference between `for-loop` and `while-loop` is that `while-loop` is used when you do not know how many iterations are required in advance.
On the other hand, `for-loop` works best when you know how many iterations will be required in advance.
ā§ for...of loop
The `for...of` loop is used to repeatedly iterate over and perform some action on each value of an iterable object, such as an array, a string, or a NodeList.
Syntax:
for (variable of iterable) {
// block of code to be executed
}
The `for...of` loop is used to repeatedly iterate over and perform some action on each value of an iterable object, such as an array, a string, or a NodeList.
Syntax:
for (variable of iterable) {
// block of code to be executed
}
ā§ for...in loop
The `for...in` loop is used to iterate through all of an object's enumerable property keys.
Syntax:
for ( variable in object ){
// block of code to be executed
}
The `for...in` loop is used to iterate through all of an object's enumerable property keys.
Syntax:
for ( variable in object ){
// block of code to be executed
}
Key points:
- `for...of` is for objects and it returns values
- `for...in` is for iterables and it returns object indices
- `for...of` is for objects and it returns values
- `for...in` is for iterables and it returns object indices
Check out this repository dedicated to frontend development, where I share informative content, tutorials, and practical code examples related to best practices in frontend development.
Check it out here: github.com
Check it out here: github.com
That's pretty much it for today. I really hope you find this thread helpful. Thank you for reading!
If you found it helpful, like and retweet the first tweet and follow me
@ishrratumar for more content.
If you found it helpful, like and retweet the first tweet and follow me
@ishrratumar for more content.
Loading suggestions...