- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between for...of and for...in statements in JavaScript?
for…in loop
The “for...in” loop is used to loop through an object's properties.
Here’s the syntax −
Syntax
for (variablename in object) { statement or block to execute }
You can try to run the following example to implement ‘for-in’ loop. It prints the web browser’s Navigator object
Example
<html> <body> <script> var aProperty; document.write("Navigator Object Properties<br /> "); for (aProperty in navigator) { document.write(aProperty); document.write("<br />"); } document.write ("Exiting from the loop!"); </script> </body> </html>
for…of loop
The “for…of” loop is used to loop through iterable objects, which includes Map, Array, arguments, etc.
Syntax
Here’s the syntax −
for (variablename of iterable){ statement or block to execute }
Example
Here’s an example showing iteration with for…of loop
<!DOCTYPE html> <html> <body> <script> let itObj= [20, 30, 40, 50]; for (let res of itObj) { res += 1; document.write("<br>"+res); } </script> </body> </html>
Output
21 31 41 51
- Related Articles
- What is the difference between for...in and for...of loops in JavaScript?
- What is the difference between break and continue statements in JavaScript?
- What is the difference between break and continue statements in C#?
- What is the difference between == and === in JavaScript?
- What is the difference between comments /*...*/ and /**...*/ in JavaScript?
- What is the difference between functions and methods in JavaScript?
- What is the difference between setTimeout() and setInterval() in JavaScript?
- What is the difference between Map and WeakMap in JavaScript?
- What is the difference between null and undefined in JavaScript?
- What is the difference between Bower and npm in JavaScript?
- What is the difference between call and apply in JavaScript?
- What is the difference between window.onload and document.onload in Javascript?
- What is the difference between getter and setter in JavaScript?
- What is the difference between a++ and ++a in JavaScript?
- What is the difference between substr() and substring() in JavaScript?

Advertisements