Skip to main content

Command Palette

Search for a command to run...

Array.flat() in JavaScript

Updated
Array.flat() in JavaScript
C

CapsCode: Empowering Innovation Through Education and Technology

CapsCode is a pioneering edtech and web development company committed to transforming the lives of students and businesses alike. Founded with the vision of creating a brighter, tech-driven future, CapsCode empowers students by providing world-class coding education while enabling businesses to achieve their goals with cutting-edge IT solutions.

For Students At CapsCode, we believe that coding is the language of tomorrow. Through engaging and industry-relevant courses, personalized mentoring, and hands-on workshops, we help students develop: Problem-solving skills: Preparing them for real-world challenges. Technical expertise: Equipping them with practical knowledge of coding and web development. Confidence in technology: Making them future-ready for careers in IT and beyond.

We foster an environment where students not only learn to code but also learn to innovate, collaborate, and think critically.

For Businesses CapsCode offers bespoke IT solutions tailored to the unique needs of businesses. From responsive web development to advanced digital marketing strategies, our services are designed to help companies grow their online presence and streamline their operations. Our key offerings include: Custom website and software development E-commerce platform creation Lead generation and digital marketing Professional email and Google Business setup

We act as partners in innovation, helping businesses leverage technology to reach new heights.

Our Mission Our mission is to empower individuals and organizations by bridging the gap between technology and accessibility. We aim to inspire a new generation of coders while helping businesses thrive in a competitive digital landscape.

Why Choose CapsCode? Over 9 years of IT industry expertise. A blend of education and professional IT services under one roof. A commitment to fostering growth, innovation, and success.

Join us in shaping the future—whether you're a student eager to unlock your potential or a business ready to embrace the possibilities of technology.

Learn. Build. Grow. With CapsCode.

Hello Devs,

In this article i will walk you through one of the important methods of Array in JavaScript i.e Array.flat method.

Table of content

  1. Theory
  2. Syntax
  3. Return Value
  4. Examples

So without wasting any time lets get into the article

Theory

The flat() method is used to flatten the array of arrays. The process of flatten depends on the depth we want to flattened the array of arrays.

What this method do is, it takes out the array element from an array of arrays and concat them in the main array. it will repeat this process till the depth value.

Syntax

Syntax:
flat(depth)
// The depth level specifying how deep a nested array structure should be flattened.
// Defaults to 1.

Return Value

return value:
A new array with the sub-array elements concatenated into it.

Examples

NOTE: IF YOU WANT TO FLATTEN THE ARRAY UPTO ITS LAST DEPTH THEN PUT depth AS Infinity

example:
let arr = [1, 2, [3, 4, [5, 6]]];

arr.flat(0) // [1, 2, [3, 4, [5, 6]]];
arr.flat(1) // [1, 2, 3, 4, [5, 6]]
arr.flat(2) // [1, 2, 3, 4, 5, 6]
arr.flat(3) // [1, 2, 3, 4, 5, 6] 
arr1.flat( ) // [1, 2, 3, 4, [5, 6]]
// if no depth is provided then it will take the default value i.e 1.
arr.flat(Infinity) //[1, 2, 3, 4, 5, 6]

arr.flat(0).length //3
arr.flat(1).length //5
arr.flat(2).length //6

Thank you for reading this far. This is a brief introduction of Array.flat() method in JavaScript . If you find this article useful, like and share this article. Someone could find it useful too. If you find anything technically inaccurate please feel free to comment below.

Hope its a nice and informative read for you. VISIT https://www.capscode.in/#/blog TO LEARN MORE... See you in my next Blog article, Take care!!

Thanks,

@capscode