JavaScript tutorials for Beginners – JavaScript for…in loop

Hits: 13


(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 loop in JavaScript allows you to iterate over all property keys of an object.

JavaScript for…in loop

The syntax of the loop is:

for (key in object) {
    // body of

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 ( let key in student ) {

    // display the properties
    console.log(`${key} => ${student[key]}`);


name => Monica
class => 7
age => 12

In the above program, the 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 ( let i in salaries) {

    // add a currency symbol
    let salary = "$" + salaries[i];

    // display the values
    console.log(`${i} : ${salary}`);


Jack : $24000,
Paul : $34000,
Monica : $55000

In the above example, the 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 loop to iterate over the string values. For example,

let string = 'code';

// using loop
for (let i in string) {



for…in with Arrays

You can also use with arrays. For example,

// define array
let arr = [ 'hello', 1, 'JavaScript' ];

// using loop
for (let x in arr) {



You will learn more about the arrays in the later tutorials.

Note: You should not use 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.



Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

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

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!