-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
for...of
Loop – The Modern Way to IterateThe for...of
loop lets you iterate over iterable objects like:
Arrays
Strings
Sets
Maps
NodeLists (in browsers)
It gives direct access to the value in each iteration, rather than the index or key.
for (let variable of iterable) {
// code block to be executed
}
variable
— A new variable representing the current value in the loop.
iterable
— A data structure that you want to loop over (like an array or string).
let colors = ["red", "green", "blue"];
for (let color of colors) {
console.log(color);
}
// Output: red green blue
let word = "Hello";
for (let char of word) {
console.log(char);
}
// Output: H e l l o
let numbers = new Set([1, 2, 3]);
for (let num of numbers) {
console.log(num);
}
let userMap = new Map([
["name", "Alice"],
["age", 30],
]);
for (let [key, value] of userMap) {
console.log(`${key}: ${value}`);
}
for...of
vs for...in
Feature | for...in |
for...of |
---|---|---|
Loops over | Object keys | Iterable values |
Use with | Objects | Arrays, strings, sets, maps |
Returns | Key/index | Actual value |
Suitable for | Objects | Arrays and other iterables |
Q1. How do you loop through all elements of an array using for...of
?
Q2. What is the output of a for...of
loop on the string "hello"
?
Q3. How can you use for...of
to iterate over a Set
object in JavaScript?
Q4. How do you use destructuring with for...of
when looping through a Map
?
Q5. What happens when you use for...of
on an object that is not iterable?
Q6. How is for...of
different from for...in
in terms of return value?
Q7. Can you use for...of
to loop through the characters of a string?
Q8. How do you skip an iteration inside a for...of
loop?
Q9. Write a for...of
loop that prints the squares of numbers in an array.
Q10. How do you iterate through values of a NodeList using for...of
in the browser?
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts