(JavaScript Tutorials for Beginners)
In this end-to-end example, you will learn – JavaScript tutorials for Beginners – JavaScript for…in loop.
JavaScript for…in loop
In this tutorial, you will learn about the JavaScript for…in loop with the help of examples.
In the previous tutorials, we have covered:
- JavaScript while and do…while loop
- JavaScript for loop
There are also other types of loops. The for..in
loop in JavaScript allows you to iterate over all property keys of an object.
JavaScript for…in loop
The syntax of the for...in
loop is:
for (key in object) {
// body of for...in
}
In each iteration of the loop, a key is assigned to the key variable. The loop continues for all object properties.
Note: Once you get keys, you can easily find its value.
Example 1: Iterate through an object
let student = {
name: 'Monica',
class: 7,
age: 12
}
// using for...in
for ( let key in student ) {
// display the properties
console.log(`${key} => ${student[key]}`);
}
Output
name => Monica class => 7 age => 12
In the above program, the for...in
loop is used to iterate over the student
object and print all its properties.
- The object key is assigned to the variable i.
student[i]
is used to access the value of i.
Example 2: Update Values of Properties
let salaries= {
Jack : 24000,
Paul : 34000,
Monica : 55000
}
// using for...in
for ( let i in salaries) {
// add a currency symbol
let salary = "$" + salaries[i];
// display the values
console.log(`${i} : ${salary}`);
}
Output
Jack : $24000, Paul : $34000, Monica : $55000
In the above example, the for...in
loop is used to iterate over the properties of the numbers
object. Then, the string $
is added to each value of the object.
for…in with Strings
You can also use for...in
loop to iterate over the string values. For example,
let string = 'code';
// using for...in loop
for (let i in string) {
console.log(string[i]);
}
Output
c o d e
for…in with Arrays
You can also use for...in
with arrays. For example,
// define array
let arr = [ 'hello', 1, 'JavaScript' ];
// using for...in loop
for (let x in arr) {
console.log(arr[x]);
}
Output
hello 1 JavaScript
You will learn more about the arrays in the later tutorials.
Note: You should not use for...in
to iterate over an array where the index order is important.
One of the better ways to iterate over an array is using the for...of
loop.
To learn more about the for...of
loop, visit JavaScript for…of loop.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.