dev-resources.site
for different kinds of informations.
Iteration Stament i.e for-of loop
Published at
8/23/2024
Categories
webdev
javascript
es6
coding
Author
mahf001
Author
7 person written this
mahf001
open
for-of:
- Introduced in ES6
- Usually a loop has counter, check-condition, updating counter. A for-of loop doesn't have anything like that.
- continue-break both can be used with it.
- Meant to give us current element.
fruits = ['banana','apple','peach','orange','mango','guava','water-melon'];
for(const item of fruits){
console.log(item);
}
'banana'
'apple'
'peach'
'orange'
'mango'
'guava'
'water-melon'
- If an array if looped over in the form of array.entries(), then the result will be each element in form of an array with index : value.
for(const item of fruits.entries()){
console.log(item);
}
[ 0, 'banana' ]
[ 1, 'apple' ]
[ 2, 'peach' ]
[ 3, 'orange' ]
[ 4, 'mango' ]
[ 5, 'guava' ]
[ 6, 'water-melon' ]
// Transform it into a single array comprising of sub-arrays:
fruits.entries(); // Object [Array Iterator] {}
[...fruits.entries()];
// [ [ 0, 'banana' ], [ 1, 'apple' ], [ 2, 'peach' ], [ 3, 'orange' ], [ 4, 'mango' ], [ 5, 'guava' ], [ 6, 'water-melon' ] ]
// Transform into a single array using for-of loop:
-> Method 1
for(const item of fruits.entries()){
console.log(`${item[0] + 1} : ${item[1]}`);
}
// '1 : banana' '2 : apple' '3 : peach' '4 : orange' '5 : mango' '6 : guava' '7 : water-melon'
-> Method 2
for(const [i,el] of fruits.entries()){
console.log(`${i + 1} : ${el}`);
}
// '1 : banana' '2 : apple' '3 : peach' '4 : orange' '5 : mango' '6 : guava' '7 : water-melon'
es6 Article's
30 articles in total
Next-Generation Buttons: Implementing the Command Pattern through Web Components
read article
Hoisting: facing Temporal dead zone
read article
Learn javascript promises. Part 1 β What is a promise?
read article
Bootcamping 02: Named exports and default exports - does it really matter?
read article
Mastering Modern JavaScript: A Deep Dive into ES6 Function Creation and Best Practices
read article
Promises: The Ability to Write Asynchronous Code Using Javascript
read article
Exploring JavaScript's Modern Primitives: BigInt and Symbol
read article
JavaScript ES6 Release Notes: Unleashing the Power of Modern JavaScript
read article
WHY YOU SHOULD LEARN ES6
read article
Understanding ES6 API's
read article
Transpiler vs Polyfills
read article
JavaScript Spread Syntax: Expanding Arrays and Objects
read article
API Design and Debugging:A Comprehensive Guide for Beginersπ
read article
Understanding the JavaScript Spread Operator (With Examples)
read article
A Comprehensive Guide to ES6 and Arrow Functions
read article
Controla tus promesa con JavaScript
read article
Sets
read article
Enhanced Object Literals
read article
Iteration Stament i.e for-of loop
currently reading
1.1 Ins & outs of ES6(JavaScript) Import with Realworld Example and Demo Project.
read article
Math Namespace & BigInt
read article
JavaScript - Destructuring Arrays & Objects [Live Doc]
read article
ES2015 (ES6) Tips, Tricks, Best Practices, and Code Snippet Examples for Your Day-to-Day Workflow
read article
Objects in JavaScript
read article
Intro to DSA & Big O Notation
read article
Execution Context & Call Stack
read article
Asynchronous programming in Javascript - Callbacks, Promises & Async Await
read article
Loops in JavaScript !!ππ
read article
Array
read article
Functions
read article
Featured ones: