Software Development Exam  >  Software Development Notes  >  JavaScript for Web Development  >  For in/ For of Loops in JavaScript

For in/ For of Loops in JavaScript | JavaScript for Web Development - Software Development PDF Download

Introduction

When working with JavaScript, loops play a crucial role in repeating a set of instructions. Two common types of loops used in JavaScript are the for-in and for-of loops. In this article, we will explore these loops and understand their differences, use cases, and how to implement them with simple examples.

The For-In Loop

The for-in loop is used to iterate over the properties of an object. It traverses through the enumerable properties of an object, including inherited properties. Let's take a look at its syntax and a couple of examples:

Syntax:

for (variable in object) {

  // code to be executed

}

Example 1: Iterating over Object Properties

const person = {

  name: 'John',

  age: 30,

  city: 'New York'

};


for (let key in person) {

  console.log(key + ':', person[key]);

}

Output

name: John

age: 30

city: New York

Explanation: In the above example, the for-in loop iterates over the properties (name, age, and city) of the person object. The key variable represents each property name, which we use to access the corresponding values (person[key]) and display them.

Example 2: Iterating over Array Indices

const fruits = ['apple', 'banana', 'orange'];


for (let index in fruits) {

  console.log(index, fruits[index]);

}

Output

0 apple

1 banana

2 orange

Explanation: In this example, the for-in loop iterates over the indices (0, 1, and 2) of the fruits array. The index variable represents each index value, which we use to access and display the corresponding array elements (fruits[index]).

The For-of Loop

The for-of loop is used to iterate over iterable objects such as arrays, strings, maps, sets, etc. It provides a simpler syntax and directly iterates over the values of the object. Let's explore its syntax and a couple of examples:

Syntax:

for (variable of iterable) {

  // code to be executed

}

Example 1: Iterating over Array Elements

const numbers = [1, 2, 3, 4, 5];


for (let number of numbers) {

  console.log(number);

}

Output

1

2

3

4

5

Explanation: In the above example, the for-of loop iterates over each element in the numbers array. The number variable represents each element's value, which we directly print using console.log().

Example 2: Iterating over String Characters

const message = 'Hello';


for (let char of message) {

  console.log(char);

}

Output

H

e

l

l

o

Explanation: Here, the for-of loop iterates over each character in the message string. The char variable represents each character, which we print using console.log().

Key Differences Between For-In and For-of Loops

  • for-in loop is used to iterate over the properties of an object, while for-of loop is used to iterate over iterable objects like arrays, strings, etc.
  • for-in loop iterates over the enumerable properties, including inherited ones, while for-of loop directly iterates over the values of the iterable object.
  • for-in loop is commonly used for objects, while for-of loop is commonly used for arrays and strings.

Download the notes
For in/ For of Loops in JavaScript
Download as PDF
Download as PDF

Sample Problems and Solutions

Problem 1: Write a for-in loop to display all the keys and values of the following object:

const car = {

  brand: 'Toyota',

  model: 'Camry',

  year: 2022

};

Solution

for (let key in car) {

  console.log(key + ':', car[key]);

}

Output

brand: Toyota

model: Camry

year: 2022

Problem 2: Write a for-of loop to calculate the sum of all numbers in the following array:

const numbers = [1, 2, 3, 4, 5];

Solution:

let sum = 0;


for (let number of numbers) {

  sum += number;

}


console.log('Sum:', sum);

Output

Sum: 15

Take a Practice Test
Test yourself on topics from Software Development exam
Practice Now
Practice Now

Conclusion

In this article, we covered the for-in and for-of loops in JavaScript. We learned that the for-in loop is used to iterate over object properties, while the for-of loop is used to iterate over iterable objects like arrays and strings. Understanding these loops and their differences will help you write more efficient and effective code. Practice implementing them with different examples to become comfortable with their usage.

The document For in/ For of Loops in JavaScript | JavaScript for Web Development - Software Development is a part of the Software Development Course JavaScript for Web Development.
All you need of Software Development at this link: Software Development
Are you preparing for Software Development Exam? Then you should check out the best video lectures, notes, free mock test series, crash course and much more provided by EduRev. You also get your detailed analysis and report cards along with 24x7 doubt solving for you to excel in Software Development exam. So join EduRev now and revolutionise the way you learn!
Sign up for Free Download App for Free
51 videos|30 docs|12 tests

Up next

51 videos|30 docs|12 tests
Download as PDF

Up next

Explore Courses for Software Development exam
Related Searches

shortcuts and tricks

,

Objective type Questions

,

For in/ For of Loops in JavaScript | JavaScript for Web Development - Software Development

,

Previous Year Questions with Solutions

,

past year papers

,

Important questions

,

Sample Paper

,

video lectures

,

Extra Questions

,

MCQs

,

Viva Questions

,

study material

,

practice quizzes

,

Semester Notes

,

pdf

,

Exam

,

ppt

,

Summary

,

For in/ For of Loops in JavaScript | JavaScript for Web Development - Software Development

,

For in/ For of Loops in JavaScript | JavaScript for Web Development - Software Development

,

mock tests for examination

,

Free

;