Home >Web Front-end >JS Tutorial >Understanding the Difference Between Iterable and Enumerable Properties in JavaScript
When working with JavaScript, you'll often need to loop through collections of data. Two common methods for doing this are the for-in and for-of loops. However, these loops are used in different contexts and for different types of collections. Understanding the distinction between iterable and enumerable properties is crucial for effectively using these loops.
The for-in loop is designed to iterate over the enumerable properties of an object. Enumerable properties are those that are visible when looping through an object or calling methods like Object.keys().
const obj = { name: 'john', age: '24' }; obj.isMarried = false; for (const key in obj) { console.log(key); // Outputs: name, age, isMarried }
In this example, the for-in loop iterates over all enumerable properties of the obj object, including the dynamically added isMarried property.
The for-of loop, on the other hand, is used to iterate over iterable objects. An iterable is an object that has a Symbol.iterator method. Common examples of iterable objects include arrays, strings, maps, and sets.
const arr = ['apple', 'pear']; arr.anotherFruit = 'banana'; for (const fruit of arr) { console.log(fruit); // Outputs: apple, pear }
Here, the for-of loop iterates over the array arr, ignoring the anotherFruit property because it is not part of the iterable sequence.
By default, arrays in JavaScript are iterable because they have a built-in Symbol.iterator method. Plain objects, however, do not have this method and are therefore not iterable.
const arr = ['apple', 'pear']; for (const fruit of arr) { console.log(fruit); // Outputs: apple, pear } const obj = { name: 'john', age: '24' }; for (const key of obj) { console.log(key); // Throws TypeError: obj is not iterable }
In the code above, attempting to use a for-of loop on an object results in a TypeError because objects do not implement the Symbol.iterator method.
When you try to use a for-of loop on a non-iterable object, JavaScript throws a TypeError. This error occurs because the object does not have the Symbol.iterator method, which is required for for-of loops.
const obj = { name: 'john', age: '24' }; try { for (const key of obj) { console.log(key); } } catch (e) { console.error(e); // Outputs: TypeError: obj is not iterable }
Below is a complete example that demonstrates the differences between the for-in and for-of loops:
const log = console.log; const arr = ['apple', 'pear']; arr.anotherFruit = 'banana'; log('Using for-of loop:'); for (const fruit of arr) { log(fruit); // Outputs: apple, pear } log('Using for-in loop:'); for (const fruit in arr) { log(fruit); // Outputs: 0, 1, anotherFruit } const obj = { name: 'john', age: '24' }; obj.isMarried = false; log('Using for-in loop:'); for (const key in obj) { log(key); // Outputs: name, age, isMarried } log('Using for-of loop:'); try { for (const key of obj) { log(key); } } catch (e) { log(e); // Outputs: TypeError: obj is not iterable }
Understanding the difference between iterable and enumerable properties is essential for effectively using JavaScript's for-in and for-of loops. The for-in loop is used for iterating over enumerable properties of objects, while the for-of loop is designed for iterating over iterable objects like arrays. Misusing these loops can lead to errors, such as the TypeError encountered when attempting to use a for-of loop on a non-iterable object. By grasping these distinctions, you can write more robust and error-free JavaScript code.
The above is the detailed content of Understanding the Difference Between Iterable and Enumerable Properties in JavaScript. For more information, please follow other related articles on the PHP Chinese website!