lynx   »   [go: up one dir, main page]

Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Basic JavaScript

JS Tutorial JS Syntax JS Variables JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Objects JS Dates JS Arrays JS Sets JS Maps JS Math JS RegExp JS Data Types JS Errors JS Debugging JS Events JS Programming JS References JS UTF-8 Characters JS Versions

JS Advanced

JS Functions JS Objects JS Classes JS Iterations JS Promises JS Modules JS HTML DOM JS Windows JS Web API JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Objects


JavaScript For In

The For In Loop

The for...in loop iterates over the enumerable properties of an object.

The for...in loop is primarily used for objects to access their property names (keys).

Syntax

for (key in object) {
  // code block to be executed
}
  • key
    A variable that holds the name (key) of each property during the iterations
  • object
    The object whose properties are being iterated over

A JavaScript for in statement loops through the properties of a person object:

Example

const person = {fname:"John", lname:"Doe", age:25};

let text = "";
for (let x in person) {
  text += person[x];
}
Try it Yourself »

Example Explained

  • The for in loop iterates over a person object
  • Each iteration returns an x (a key)
  • The x is used to access the value of x
  • The value of x is person[x]

For In Over Arrays

The JavaScript for in statement can also loop over the properties of an Array:

Syntax

for (variable in array) {
  code
}

Example

const numbers = [45, 4, 9, 16, 25];

let txt = "";
for (let x in numbers) {
  txt += numbers[x];
}
Try it Yourself »

Control Flow

Like in other JavaScrpt loops, you can use control flow statements inside the loop:

  • break
    break the loop execution and go to the statements after the loop
  • continue
    break the loop execution and go to the next iteration of the loop

Enumerable Properties

The for...in loop only iterates over enumerable properties.

Properties with the enumerable attribute is set to false, like some built-in methods or properties defined with Object.defineProperty() will not be included.


The Prototype Chain

The for...in loop also iterates over enumerable properties inherited from the object's prototype chain. To avoid this, you can use hasOwnProperty() inside the loop to check if the property belongs directly to the object itself.



Note

Do not use for in over an Array if the index order is important.

The index order is engine-dependent, and array values may not be accessed in the order you expect.

It is better to use a for loop, a for of loop, or Array.forEach() when the order is important.

Array.forEach()

The forEach() method calls a function (a callback function) once for each array element.

Example

const numbers = [45, 4, 9, 16, 25];

let txt = "";
numbers.forEach(myFunction);

function myFunction(value, index, array) {
  txt += value;
}
Try it Yourself »

Note that the function takes 3 arguments:

  • The item value
  • The item index
  • The array itself

The example above uses only the value parameter. It can be rewritten to:

Example

const numbers = [45, 4, 9, 16, 25];

let txt = "";
numbers.forEach(myFunction);

function myFunction(value) {
  txt += value;
}
Try it Yourself »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

Лучший частный хостинг