
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
What is the difference between for...in and for...of loops in JavaScript?
Difference between for...in and for...of loops
Both the loops iterate over something. The main difference between them is in what they iterate over.
1) for...in loop
This loop iterates over enumerable properties of an object in an arbitrary order. It cares only about properties but not values.
In the following example by using for...in loop the properties of the array are iterated. Since it is an array, Index is an important property so index of each and every element will be iterated and displayed in the output. In addition to indexes some inherited properties such as "inherProp2", "inherProp1" are also displayed.
Example-1
<html> <body> <script> Object.prototype.inherProp1 = function() {}; Array.prototype.inherProp2= function() {}; var org = ["Apple", "Microsoft", "Tesla"] org.anotherOrg = "solarCity"; for (var key in org) { document.write(key); document.write("</br>"); } </script> </body> </html>
Output
0 1 2 anotherOrg // own property inherProp2 // inherited property inherProp1 // inherited property
In the following example, since hasOwnProperty() is used, only own properties such as indexes and other properties are displayed where as inherited properties such as "inherProp1" and "inherProp2" are not displayed.
Example-2
<html> <body> <script> Object.prototype.objCustom = function() {}; Array.prototype.arrCustom = function() {}; var org = ["Apple", "Microsoft", "Tesla"] org.anotherOrg = "solarCity"; for (var i in org) { if (org.hasOwnProperty(i)) { document.write(i); document.write("</br>"); } } </script> </body> </html>
Output
0 1 2 anotherOrg
2) For...of loop
Unlike for...in loop, for...of loop iterates over values that the object defines to be iterated over.
In the following example the properties such as 'Apple', 'Microsoft' and 'Tesla' are displayed in the output using for...of loop.
Example
<html> <body> <script> var org = ["Apple", "Microsoft", "Tesla"] for (var key of org) { document.write(key); document.write("</br>"); } </script> </body> </html>
Output
Apple Microsoft Tesla
- Related Articles
- What is the difference between for...of and for...in statements in JavaScript?
- For Loops in Javascript
- What is basic syntax of Python for Loops?
- For and While loops in Arduino
- Reverse array with for loops JavaScript
- What is the difference between == and === in JavaScript?
- What are the best practices for using loops in Python?
- 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 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 substr() and substring() in JavaScript?
