JavaScript Array flat() Method17 Mar 2025 | 2 min read The flat() 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 where we need to flatten the array. By default, the depth limit is 1. SyntaxParametersDepth: It is an optional parameter which specifies the depth to flatten an array. By default, its value is 1. ReturnIt returns a newly created array containing all the sub-array elements concatenated into it. JavaScript Array flat() Method ExampleLet's see the below examples to understand better. Example1 A simple working of flat() method on a two dimensional array. Test it NowOutput: ![]() Example2 Testing a multidimensional array with flat() method. Test it NowOutput: It is clear that each element in the array is concatenated to the newly created 1D array. ![]() Example3 Let's flatten an array upto a specified depth. Test it NowOutput: ![]() Example4 Using flat() method with depth value as infinity. Test it NowOutput: ![]() Example5 Let's flatten an array with holes in between. Test it NowOutput: ![]() It is clear that all array elements are concatenated leaving the holes out, after applying the flat() method. Next TopicJavaScript Array |
JavaScript Array Method The method is used for converting and representing an array into string form. It returns the string containing the specified array elements. Commas separate these elements, and the string does not affect the original array. Syntax The following syntax represents the method: array. Parameters It...
3 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
JavaScript Array method The JavaScript array method fills the elements of the given array with the specified static values. This method modifies the original array. It returns undefined, if no element satisfies the condition. Syntax The method is represented by the following syntax: arr.fill(value[, start[, end]]) Parameter value...
1 min read
JavaScript Array method The JavaScript array method is used to invoke the specified function once for each array element. Syntax The method is represented by the following syntax: array.forEach(callback(currentvalue,index,arr),thisArg) Parameter callback - It represents the function that test the condition. currentvalue - The current element of array. index - It...
1 min read
JavaScript Array method The JavaScript array method adds one or more elements to the end of the given array. This method changes the length of the original array. Syntax The method is represented by the following syntax: array.push(element1,element2....elementn) Parameter element1,element2....elementn - The elements to be added. Return The original array...
1 min read
JavaScript Array Method The method creates and returns a new array from different a number of arguments. It does not focus on the type and number of arguments. Similar to Array, it also provides a constructor that handles the integer arguments, but in a...
2 min read
JavaScript Array Method The method creates a new array iterator object that carries the values specified at each array index. We can iterate the array elements via loops or iterator methods. Syntax array.; Parameter It does not hold any parameter as such. Return It creates and returns a newly created...
2 min read
JavaScript Array method The JavaScript array method extracts the part of the given array and returns it. This method doesn't change the original array. Syntax The method is represented by the following syntax: array.slice(start,end) Parameter start - It is optional. It represents the index from where the method...
1 min read
JavaScript Array method The JavaScript array method adds one or more elements in the beginning of the given array and returns the updated array. This method changes the length of the original array. Syntax The method is represented by the following syntax: array. unshift(element1,element2,....,elementn) Parameter element1,element2,....,elementn - The...
1 min read
JavaScript Array Method The methods perform testing and checks if atleast a single array element passes the test, implemented by the provided function. If the test is passed, it returns true. Else, returns false. Note: If method is applied on an empty array, it...
2 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