"For in" loop in JavaScript

How does “for in” loop work in JavaScript?

Last updated:7/19/2021 5:36:51 AM

1 Answers

Ethan Karla
Ethan Karla

The for..in statement are just same as the foreach loop in PHP if you know. And if you don't know no worry, here you'll understand that.

for...in loops are the iterations which is used basically for executing the whole array from 0th to nth index. If you want to execute an array from 0 to last index then for...in loop will be the best prospective to achieve this.

Let's understand it with the help of an example :

<!DOCTYPE html>

<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <meta charset='utf-8' />
    <title>Switch Case Statement</title>
    <script>
        const object = { a: 1, b: 2, c: 3 };
        for (const property in object) {
            console.log(`${property}: ${object[property]}`);
        }
    </script>
</head>
<body align='center'>
</body>
</html>

Hope this information has cleared your confusion.

Happy Coding!


Answer