for ... in ... in JavaScript is used to iterate over the enumerable properties of an object, it iterates over the keys (property names) instead of the values. Steps: Declare a variable key to store the current key. Specify the object to be traversed after the in keyword. The loop will traverse each enumerable property of the object and execute the code within the curly braces on each key.
How to use for ... in JavaScript
for ... in is a loop statement , used to iterate over the enumerable properties of an object. It iterates over the keys (property names) of the object, not the values.
Syntax
<code>for (let key in object) { // 对每个键执行代码 }</code>
Steps
key
, used to store the current key. in
keyword. Example
<code>const person = { name: "John Doe", age: 30, city: "New York" }; for (let key in person) { console.log(key); // 输出:name, age, city }</code>
Notes
for ... in
The loop traverses the keys of the object, not the value. for ... in
loop will iterate over the array's indices, not the element values. for ... in
loop will iterate over the properties from the parent object. The above is the detailed content of How to use for in in js. For more information, please follow other related articles on the PHP Chinese website!