JavaScript Array keys() Method17 Mar 2025 | 2 min read The keys() method creates and returns a new iterator object which holds the key for every index in the array. This method does not affect the original array. SyntaxParameterIt does not hold any parameter. ReturnIt returns a new array iterator object. JavaScript Array keys() ExampleLet's see some examples to understand better. Example1 Here's the simple implementation of keys() method in array. Test it NowOutput: ![]() So, according to the number of elements present in the array, a key is assigned to each one. Example2 Implementing keys() method with holes in between the array. Test it NowOutput: ![]() Note: It is clear from the above example that Array keys() method does not ignore the holes present as an array element in the given array. It assigns a key value to that empty hole too. Also, the keys are assigned in an increasing order sequence to each element.Example3 Test it NowOutput: ![]() Example4 Here's an example to generate keys for a user-define array. Test it NowOutput: Initially, the user will input the elements through prompt box which will be displayed as: ![]() After clicking on Get Keys button, the keys will be generated as: ![]() Thus, keys() method is a direct approach forgenerating keys for the array values. Next TopicJavaScript Array |
JavaScript Array method The JavaScript array method is used to search the position of a particular element in a given array. It behaves similar to indexOf() method with a difference that it start searching an element from the last position of an array. The ...
1 min read
JavaScript Array Method The method creates a new array that holds the shallow copy from an array or iterable object. When applied to a string, each word gets converted to an array element in the new array. Syntax There is a following possible syntax: Array.from(object,map_fun,thisArg); Parameter object: It is...
2 min read
JavaScript Array copyWithin() method The JavaScript array copyWithin() method copies the part of the given array with its own elements and returns the modified array. This method doesn't change the length of the modified array. Syntax The copyWithin() method is represented by the following syntax: array.copyWithin(target, start, end) Parameter target -...
1 min read
JavaScript Array Method The method is an inbuilt array method that flattens a given array into a newly created one-dimensional array. It concatenates all the elements of the given multidimensional array, and flats upto the specified depth. We can specify the depth limit to...
2 min read
JavaScript Array method The JavaScript array method combines all the elements of an array into a string and return a new string. We can use any type of separators to separate given array elements. Syntax The method is represented by the following syntax: array.join(separator) Parameter Separator() - It...
1 min read
JavaScript Array Method The method creates a string that represents the elements of an array. It converts the array elements into the string. Syntax array.; Parameter array: It is the given array or the source array. Including this, there are two optional parameters also. locales: It is a string that...
2 min read
JavaScript Array reduce() Method The reduce() method reduces the given array into a single value by executing a reducer function. The user implements the reducer function that works on every element present in the array. Reducer Function The reducer function is the user implemented code. It uses the...
2 min read
JavaScript Array Method The method is a combination of flat() and map() methods. This method initially maps each array element through a mapping function, then flatten up the array with depth value as 1. Syntax var newArr=arr.flatMap(function callback(currentValue[ , index[ , array]]) { return element } [ , thisArg]) Parameters callback-...
2 min read
JavaScript Array Method The JavaScript array method combines two or more arrays and returns a new string. This method doesn't make any change in the original array. Syntax The method is represented by the following syntax: array.concat(arr1,arr2,....,arrn) Parameter arr1,arr2,....,arrn - It represent the arrays to be combined. Return A new...
1 min read
JavaScript Array method The JavaScript array method returns the first element of the given array that satisfies the provided function condition. Syntax The method is represented by the following syntax: array.find(callback(value,index,arr),thisArg) Parameter callback - It represents the function that executes each element. value - The current element...
1 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India