The Wayback Machine - https://web.archive.org/web/20241007005218/https://www.geeksforgeeks.org/javascript-array-values-method/
Open In App

JavaScript Array values() Method

Last Updated : 09 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

JavaScript array.values() is an inbuilt method in JavaScript that is used to return a new array Iterator object that contains the values for each index in the array i.e., it prints all the elements of the array. 

Syntax

arr.values();

Return value:

  • It returns a new array iterator object i.e., elements of the given array. 

Example 1: Printing values of array using array values() method

Here, we will print the values of an array using the array values() method. 

JavaScript
// Input array contain some elements
let A = ['Ram', 'Z', 'k', 'geeksforgeeks'];

// Here array.values() method is called.
let iterator = A.values();

// All the elements of the array the array 
// is being printed.
console.log(iterator.next().value);
console.log(iterator.next().value);
console.log(iterator.next().value);
console.log(iterator.next().value);

Output
Ram
Z
k
geeksforgeeks

Explanation:

  • The values() method is called on the array A.
  • An iterator object iterator is obtained from the array.
  • Each call to iterator.next() returns an object with a value property containing the next element in the array.
  • The value property of each iterator’s result object is logged to the console to print each element of the array sequentially.

Example 2: array values() method with for loop

Here, we will be using the array values() method with for loop to print the values of an array. 

JavaScript
// Input array contain some elements.
let array = ['a', 'gfg', 'c', 'n'];

// Here array.values() method is called.
let iterator = array.values();

// Here all the elements of the array is being printed.
for (let elements of iterator) {
    console.log(elements);
}

Output
a
gfg
c
n

Explanation:

  • The array array contains elements 'a', 'gfg', 'c', and 'n'.
  • An iterator iterator is obtained from the array using the values() method.
  • The for...of loop iterates over the elements returned by the iterator.
  • Each element elements is logged to the console.
  • The output will be 'a', 'gfg', 'c', and 'n'.

Example 3: Printing elements of array with holes using array values() method

Here, we have used value() method in Arrays with Holes.

JavaScript
let array = ["A", "B", , "C", "D"];

let iterator = array.values();

for (let value of iterator) {
  console.log(value);
}

Output
A
B
undefined
C
D

Explanation:

  • The array array contains elements "A", "B", an empty slot, "C", and "D".
  • An iterator iterator is obtained from the array using the values() method.
  • The for...of loop iterates over the elements returned by the iterator.
  • Each element value is logged to the console.
  • The output will be "A", "B", "C", and "D". The empty slot will not produce any output.

We have a complete list of JavaScript Array methods, to check those please go through this Javascript Array Complete reference article.

Supported Browser:

JavaScript Array values() Method- FAQ’s

What does Array.prototype.values() do in JavaScript?

Array.prototype.values() returns an iterator object containing the values of an array, allowing sequential access to each element’s value via the iterator’s next() method.

Does values() modify the original array?

No, the values() method in JavaScript does not modify the original array. It returns an iterator object that allows sequential access to the values of the array, but it does not change the array itself.

When should I use JavaScript values() over other iteration methods?

Use values() when you need to iterate over array values sequentially without modifying the array or accessing indices. It’s handy for simple value iteration and works well with for…of loops.

Does values() work with sparse arrays or arrays with holes?

Yes, values() works with sparse arrays and handles array elements with undefined or uninitialized values as expected, iterating over defined elements only.

Can values() be used to modify or delete array elements?

No, values() only provides read-only access to array values via an iterator. It does not allow modification, deletion, or addition of elements.




Previous Article
Next Article

Similar Reads

How to map array values without using map method in JavaScript ?
Array elements can be mapped by using looping methods in JavaScript. The map() method creates a new array with the results of the output of a function called for each array element. This can also be implemented using a for loop in JavaScript. Approach 1: For this, we can create two arrays, in which one array contains the array elements that are to
3 min read
Filter Nested Array In Object Array By Array Of Values Using Lodash
When we work with complex data structures such as arrays and objects in JavaScript, we might face a situation where we want to filter a nested array within an object array based on a specific set of values. We can do this by using Lodash, a popular JavaScript utility library. These are the following approaches to Filter Nested Array In Object Array
3 min read
What is the use of the Values method in JavaScript Sets ?
In JavaScript Sets, the values() method is used to return a new iterator object that contains the values for each element in the Set, in insertion order. The iterator object follows the iterable protocol, allowing you to loop through the values using methods like next(). Syntax:mySet.values()mySet: The Set for which you want to obtain an iterator f
1 min read
JavaScript TypedArray.prototype.values() Method
TypedArray's values() method returns a new iterator object for the array, this iterator will allow you to traverse through typed arrays and generate the value of each element at every position and like values() method for normal JavaScript arrays, it serves a similar role. SyntaxtypedArray.values()Parameter: Does not accept any parameter. Return Va
1 min read
JavaScript Object values() Method
JavaScript object.values() method is used to return an array whose elements are the enumerable property values found on the object. The ordering of the properties is the same as that given by the object manually if a loop is applied to the properties. Object.values() takes the object as an argument of which the enumerable property values are to be
3 min read
JavaScript Map values() Method
The JavaScript map.values() method is used to return a new Iterator object that contains the value of each element present in Map. The order of the values are in the same order that they were inserted into the map. Syntax:myMap.values()Parameters: This method does not accept any parameters.Example 1: [GFGTABS] JavaScript <script> let myMap =
2 min read
JavaScript Set values() Method
The Set.values() method in JavaScript returns a new Iterator object that contains all of the items available in the set in a certain order. The order of values are in the same order that they were inserted into the set. Syntax:mySet.values()Parameters:This method does not accept any parameters.Return Value:The Set.values() method returns a new iter
3 min read
How to create an array with random values with the help of JavaScript ?
The task is to generate an array with random values with the help of JavaScript. There are two approaches which are discussed below: Approach 1: Use Math.random() and Math.floor() methods to get the random values.Push the values one by one in the array (But this approach may generate repeated values). Example: This example implements the above appr
2 min read
How to filter values from an array for which the comparator function does not return true in JavaScript ?
The task is to filter the array based on the returned value when passed to the given function. The purpose is to loop through the array and execute one function that will return true or false. Then filter out all the values for which the function (comparator function) does not return true. These are the following methods for filtering the values fr
3 min read
How to fill static values in an array in JavaScript ?
In this article, we will see the methods to fill an array with some static values. There are many ways to fill static values in an array in JavaScript such as: Using Array fill() MethodUsing for loopUsing push() methodUsing from() methodUsing spread operatorMethod 1: Array fill() Method We use the array fill() method to fill static values in an arr
5 min read
Create an array filled with given values Using JavaScript
In JavaScript, an array is a collection of values that can be stored and accessed in a single variable. Each value in an array is called an element, and elements can be of any data type, such as numbers, strings, or other objects. Arrays are indexed, meaning that each element has a numerical position within the array. JavaScript arrays are dynamic,
5 min read
Implement a Function that Accept Array and Condition and Returns Boolean Values in JavaScript
In JavaScript language every function is a special function that checks the element of the array with the condition given in the input, Each element is checked against the condition, and if the condition is satisfied then every function returns the true result else if any of the element of the array fails against the condition then the false result
2 min read
How to find property values from an array containing multiple objects in JavaScript ?
To find property values from an array containing multiple objects in JavaScript, we have multiple approaches. In this article, we will learn how to find property values from an array containing multiple objects in JavaScript. Several methods can be used to find property values from an array containing multiple objects. Table of Content Using for-lo
4 min read
How to get Values from Specific Objects an Array in JavaScript ?
In JavaScript, an array is a data structure that can hold a collection of values, which can be of any data type, including numbers, strings, and objects. When an array contains objects, it is called an array of objects. Table of Content Using the forEach() methodUsing the map() methodUsing the filter() methodUsing the forEach() methodThe forEach()
2 min read
Sorting an Array of Binary Values using JavaScript
JavaScript allows us to sort an array containing only binary values i.e. 0 and 1, below is an example to understand the problem. Example: Input array = [ 0 , 1, 0, 0 , 0, 1, 0 , 1, 0 , 1, 1]Output array = [ 0 , 0 , 0 , 0 , 0, 0 , 1 , 1, 1, 1]Below are several approaches to sorting an array of binary values using JavaScript which are as follows: Tab
2 min read
Find all the combinations of the array values in JavaScript
The task is to get the Cartesian product of the arrays (Finding all combinations after concatenating them). Here, 2 approaches are discussed with the help of JavaScript. Table of Content Approach 1: Using JavaScript Array.reduce() methodApproach 2: Using recursive approachApproach 3: Iterative Method Approach 1: Using JavaScript Array.reduce() meth
3 min read
How to remove falsy values from an array in JavaScript ?
Falsy/Falsey Values: In JavaScript, there are 7 falsy values, which are given below falsezero(0,-0)empty string("", ' ' , ` `)BigIntZero(0n,0x0n)nullundefinedNaNIn JavaScript, the array accepts all types of falsy values. Let's see some approaches on how we can remove falsy values from an array in JavaScript: Using for-each loopUsing the Array.filte
6 min read
How to convert a 2D array to a comma-separated values (CSV) string in JavaScript ?
Given a 2D array, we have to convert it to a comma-separated values (CSV) string using JS. Input:[ [ "a" , "b"] , [ "c" ,"d" ] ]Output:"a,b c,d"Input:[ [ "1", "2"]["3", "4"]["5", "6"] ]Output:"1,23,45,6"To achieve this, we must know some array prototype functions which will be helpful in this regard: Join function: The Array.prototype.join( ) funct
4 min read
How to filter out the non-unique values in an array using JavaScript ?
In JavaScript, arrays are the object using the index as the key of values. In this article, let us see how we can filter out all the non-unique values and in return get all the unique and non-repeating elements. These are the following ways by which we can filter out the non-unique values in an array: Table of Content Using the filter() methodUsing
4 min read
JavaScript Program to Sort an Associative Array by its Values
In Javascript, "arrays" are indexed by numerical values, while "objects" are essentially what you might refer to as associative arrays. It's important to note that "objects" do not have a guaranteed order. In this article, we will learn how can we sort an associative array by its values. Table of Content Using sort() methodUsing Object.entries meth
4 min read
JavaScript Program to Create an Array of Unique Values From Multiple Arrays Using Set Object
We have given multiple arrays consisting of numerical values, and our task is to create an output array that consists of unique values from the multiple input arrays. We will use the Set object in JavaScript language. Example: Input: inputarray1: [1,2,3,4,5] inputarray2: [4,5,6,7,8] Output: outputArray: [1,2,3,4,5,6,7,8]Table of Content Using Set a
5 min read
How to Create Array of Objects From Keys & Values of Another Object in JavaScript ?
Creating an array of objects from the keys and values of another object involves transforming the key-value pairs of the original object into individual objects within an array. Each object in the array represents a key-value pair from the source object. Below approaches can be used to accomplish this taskTable of Content Using Object.keys() and re
3 min read
JavaScript Get all non-unique values from an array
We have given a JavaScript array, and the task is to find all non-unique elements of the array. To get all non-unique values from the array here are a few examples. These are the following methods to get all non-unique values from an array: Table of Content Using Array Slice() MethodUsing for loopUsing filter() MethodUsing indexOf() MethodUsing red
4 min read
How to Create a Dropdown List with Array Values using JavaScript?
To add elements to the select element from the JavaScript array, We can also get selected values in the dropdown list using JavaScript. Here we will populate the dropdown list with an array. Below are the methods to create a dropdown list with array values: Table of Content Using Pure JavaScriptUsing jQueryUsing Pure JavaScriptUtilizes pure JavaScr
2 min read
JavaScript Program to Check if an Array Contains only Unique Values
In this article, we are given an array, Our task is to find whether the elements in an array are unique or not. Examples: Input 1: 7,8,1,5,9 Output: true Input2: 7,8,1,5,5 Output: falseIn Input 1, elements 7,8,1,5,9 are distinct from each other and they were unique, there was no repetition of elements hence the array contains unique values. So the
4 min read
How to get distinct values from an array of objects in JavaScript?
Getting distinct values from an array of objects in JavaScript involves extracting unique values from a specific property across all objects. This process eliminates duplicates, providing a clean and concise representation of the unique values present within the array. Pre-requisite: Array of Objects in JavaScript Example: Input:[ { name: "Ram", ag
6 min read
JavaScript Program to Create an Array with a Specific Length and Pre-filled Values
In JavaScript, we can create an array with a specific length and pre-filled values using various approaches. This can be useful when we need to initialize an array with default or placeholder values before populating it with actual data. Table of Content Method 1: Using the Array() Constructor and fill() MethodMethod 2: Using a Loop to Initialize V
3 min read
How to Swap Two Array of Objects Values using JavaScript ?
Swapping values between two arrays of objects is a common operation in JavaScript, there are multiple methods to swap values between arrays of objects efficiently. Swapping values between arrays can be useful in scenarios like reordering data, shuffling items, or performing certain algorithms. These are the following approaches:Table of Content Usi
6 min read
How to Find Property Values in an Array of Object using if/else Condition in JavaScript ?
Finding property values in an array of objects using if/else condition is particularly useful when there is a collection of objects. Table of Content Using Array Find MethodUsing Array Filter MethodUsing For Of LoopUsing Array Map MethodUsing Array Reduce MethodUsing Array Some MethodUsing Array Find MethodThe array find method loop in JavaScript i
5 min read
How to Swap Array Object Values in JavaScript ?
We have given the array of objects, and our task is to swap the values of the object keys present in the array of objects. Below is an example for a better understanding of the problem statement. Example:Input: arr = [{a: 1, b: 2}, {a:3, b: 4}]Output: [ { a: 2, b: 1 }, { a: 4, b: 3 } ]Explnation: The values of the object keys a and b before swappin
4 min read