|
| 1 | +--- |
| 2 | +Title: '.values()' |
| 3 | +Description: 'Returns a new array iterator object that contains the values of each element in the array.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Web Development' |
| 7 | +Tags: |
| 8 | + - 'Arrays' |
| 9 | + - 'JavaScript' |
| 10 | + - 'Methods' |
| 11 | +CatalogContent: |
| 12 | + - 'introduction-to-javascript' |
| 13 | + - 'paths/front-end-engineer-career-path' |
| 14 | +--- |
| 15 | + |
| 16 | +In JavaScript, the **`.values()`** method returns a new array [iterator](https://www.codecademy.com/resources/docs/javascript/iterators) object containing the values of each element in the array. This method enables explicit iteration using a `for...of` loop or the iterator's `.next()` method. |
| 17 | + |
| 18 | +> **Note:** The iterable returned by `.values()` is _not reusable_. Once it has been fully consumed (i.e., all elements have been iterated over), it becomes exhausted. To iterate again, a new iterator must be created by calling `.values()` again on the array. |
| 19 | +
|
| 20 | +## Syntax |
| 21 | + |
| 22 | +```pseudo |
| 23 | +array.values(); |
| 24 | +``` |
| 25 | + |
| 26 | +**Parameters:** |
| 27 | + |
| 28 | +The `.values()` method does not take any parameters. |
| 29 | + |
| 30 | +**Return value:** |
| 31 | + |
| 32 | +Returns a new array iterator object containing the values of each index in the array, in order. |
| 33 | + |
| 34 | +## Example 1: Using a `for...of` Loop |
| 35 | + |
| 36 | +This example uses a `for...of` loop to iterate over the iterable object returned by `.values()` and prints the values: |
| 37 | + |
| 38 | +```js |
| 39 | +const fruits = ['apple', 'banana', 'orange']; |
| 40 | +const iterator = fruits.values(); |
| 41 | + |
| 42 | +for (const value of iterator) { |
| 43 | + console.log(value); |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +The code will produce this output: |
| 48 | + |
| 49 | +```shell |
| 50 | +apple |
| 51 | +banana |
| 52 | +orange |
| 53 | +``` |
| 54 | + |
| 55 | +## Example 2: Using the Iterator's `.next()` Method |
| 56 | + |
| 57 | +This example uses the `.next()` method to manually iterate through the values and print them to the console: |
| 58 | + |
| 59 | +```js |
| 60 | +const numbers = [2, 4, 6, 8]; |
| 61 | +const iterator = numbers.values(); |
| 62 | + |
| 63 | +console.log(iterator.next().value); |
| 64 | +console.log(iterator.next().value); |
| 65 | +console.log(iterator.next().value); |
| 66 | +console.log(iterator.next().value); |
| 67 | +console.log(iterator.next().value); |
| 68 | +``` |
| 69 | + |
| 70 | +The code will produce this output: |
| 71 | + |
| 72 | +```shell |
| 73 | +2 |
| 74 | +4 |
| 75 | +6 |
| 76 | +8 |
| 77 | +undefined |
| 78 | +``` |
| 79 | + |
| 80 | +Each time `.next()` is called, it gives an object with: |
| 81 | + |
| 82 | +- `value`: The current index value. |
| 83 | +- `done`: A boolean that tells if there are more items left. |
| 84 | + |
| 85 | +The last `console.log()` prints `undefined` because there are no more values to iterate over. |
| 86 | + |
| 87 | +## Codebyte Example |
| 88 | + |
| 89 | +In this codebyte example, the `.values()` method returns an iterator for the `fruits` array. The iterator is used to access each element until the iteration is done: |
| 90 | + |
| 91 | +```codebyte/javascript |
| 92 | +const fruits = ['apple', 'banana', 'orange']; |
| 93 | +const iterator = fruits.values(); |
| 94 | +
|
| 95 | +let result = iterator.next(); |
| 96 | +
|
| 97 | +while (!result.done) { |
| 98 | + console.log(`Fruit: ${result.value}`) |
| 99 | + result = iterator.next(); |
| 100 | +} |
| 101 | +
|
| 102 | +console.log('Iteration completed.'); |
| 103 | +``` |
| 104 | + |
| 105 | +The `while` loop keeps running as long as `done` is `false`. Inside the loop, it prints each fruit. When all items have been printed, the loop stops, and a final message is shown. |
0 commit comments