2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: For...In Loop
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
									
										
										
										
											2018-11-14 20:57:23 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `for...in`  statement iterates over the enumerable properties of an object, in an arbitrary order. For each distinct property, statements can be executed.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								for (variable in object) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								| Required/Optional | Parameter | Description |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|-------------------|-----------|----------------------------------------------------------------------|
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								| Required | Variable | A different property name is assigned to variable on each iteration. |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								| Optional | Object | Object whose enumerable properties are iterated. |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Examples
  
						 
					
						
							
								
									
										
										
										
											2018-11-14 20:57:23 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Initialize object.
							 
						 
					
						
							
								
									
										
										
										
											2018-12-23 09:04:10 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const a = { "a": "Athens", "b": "Belgrade", "c": "Cairo" };
							 
						 
					
						
							
								
									
										
										
										
											2018-11-14 20:57:23 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Iterate over the properties.
							 
						 
					
						
							
								
									
										
										
										
											2018-12-23 09:04:10 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								let s = "";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								for (let key in a) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  s += key + ": " + a[key];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  s += "< br  / > ";
							 
						 
					
						
							
								
									
										
										
										
											2018-11-14 20:57:23 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2018-12-23 09:04:10 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-14 20:57:23 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								document.write (s);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-14 20:57:23 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Output:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// a: Athens
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// b: Belgrade
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// c: Cairo
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-14 20:57:23 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Initialize the array.
							 
						 
					
						
							
								
									
										
										
										
											2018-12-23 09:04:10 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								let arr = new Array("zero", "one", "two");
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-14 20:57:23 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Add a few expando properties to the array.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								arr["orange"] = "fruit";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								arr["carrot"] = "vegetable";
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-14 20:57:23 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Iterate over the properties and elements.
							 
						 
					
						
							
								
									
										
										
										
											2018-12-23 09:04:10 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								let s = "";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								for (let key in arr) {
							 
						 
					
						
							
								
									
										
										
										
											2018-11-14 20:57:23 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    s += key + ": " + arr[key];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    s += "< br  / > ";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-14 20:57:23 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								document.write (s);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-14 20:57:23 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Output:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//   0: zero
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//   1: one
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//   2: two
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//   orange: fruit
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//   carrot: vegetable
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-14 20:57:23 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Efficient way of getting an object's keys using an expression within the for-in loop's conditions
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								var myObj = {a: 1, b: 2, c:3}, myKeys = [], i=0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								for (myKeys[i++] in myObj);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-14 20:57:23 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								document.write(myKeys);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-14 20:57:23 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								//Output:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//   a
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//   b
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//   c
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Other Resources:
  
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								*  [MDN link ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in ) 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*  [MSDN link ](https://msdn.microsoft.com/library/55wb2d34.aspx )