java-script:for-in-vs-for-of
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| java-script:for-in-vs-for-of [2023/08/09 23:52] – created odefta | java-script:for-in-vs-for-of [2025/01/02 18:22] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 18: | Line 18: | ||
| </ | </ | ||
| - | ==== Example with an array ==== | + | ==== Example with an Array ==== |
| <code javascript> | <code javascript> | ||
| const arr = [10, 20, 30]; | const arr = [10, 20, 30]; | ||
| Line 51: | Line 51: | ||
| </ | </ | ||
| + | ===== For Of ===== | ||
| + | |||
| + | The **for...of** loop ;;;iterates over the values;;; of an iterable object (such as an **array, set, or map**). | ||
| + | It gives you the values of the iterable directly. \\ | ||
| + | <note warning> | ||
| + | It can't be used directly on plain objects without using methods like Object.keys, | ||
| + | </ | ||
| + | |||
| + | ==== Example with an Array ==== | ||
| + | |||
| + | <code javascript> | ||
| + | const arr = [10, 20, 30]; | ||
| + | |||
| + | for (const value of arr) { | ||
| + | console.log(value); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Example with an Object ==== | ||
| + | |||
| + | The **for...of** loop is designed to work with iterable objects, such as arrays, strings, maps, sets, etc. Ordinary objects are not iterable by default, so **attempting to use for...of with an object will lead to an error.** | ||
| + | |||
| + | However, you can convert an object' | ||
| + | Example: | ||
| + | <code javascript> | ||
| + | const person = { | ||
| + | name: ' | ||
| + | age: 30, | ||
| + | profession: ' | ||
| + | }; | ||
| + | |||
| + | // Iterate over keys | ||
| + | for (const key of Object.keys(person)) { | ||
| + | console.log(`Key: | ||
| + | } | ||
| + | |||
| + | // Iterate over values | ||
| + | for (const value of Object.values(person)) { | ||
| + | console.log(`Value: | ||
| + | } | ||
| + | |||
| + | // Iterate over key-value pairs | ||
| + | for (const [key, value] of Object.entries(person)) { | ||
| + | console.log(`Key: | ||
| + | } | ||
| + | |||
| + | // Output: | ||
| + | // Key: name | ||
| + | // Key: age | ||
| + | // Key: profession | ||
| + | // Value: Alice | ||
| + | // Value: 30 | ||
| + | // Value: Engineer | ||
| + | // Key: name, Value: Alice | ||
| + | // Key: age, Value: 30 | ||
| + | // Key: profession, Value: Engineer | ||
| + | </ | ||
| + | |||
| + | In this example, **Object.keys(person)**, | ||
| + | |||
| + | ===== Conclusion ===== | ||
| + | |||
| + | * Use **for...in** to iterate over the **keys** of an **object** (including non-array objects). | ||
| + | * Use **for...of** to iterate over the **values** of iterables like **arrays, sets, and maps**. | ||
| + | * Be cautious using **for...in with arrays**, especially if you have added custom properties, as it can lead to unexpected results. | ||
java-script/for-in-vs-for-of.1691625125.txt.gz · Last modified: (external edit)
