-
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 Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
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 Canvas
JS Graphics & Charts
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 Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
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 Canvas
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?
Q1: What does the for...of loop iterate over?
Q2: Which loop is best suited for looping through an array?
Q3: Which of the following is NOT iterable?
Q4: What is the result of looping over a Set using for...of?
Q5: What error occurs if you use for...of on a non-iterable object?
Q6: Which one correctly loops through a Map's entries using for...of?
Q7: Can you use continue inside a for...of loop?