Exambodh - Practice Aptitude, Reasoning & GK Questions

Back to Articles
JavascriptJavascript

JavaScript Arrays Explained with Examples Part 2 (Complete Beginner Guide)

Javascript·

Share Article

Share this article on social platforms or copy the direct link.

JavaScript Arrays Cheat Sheet – Part 2

If you already understand basic array methods, the next step is learning how to work with data more efficiently. In this part, we’ll go beyond the basics and look at methods that help you reshape, inspect, and transform arrays in a cleaner way.

These methods are especially useful when dealing with real-world data, API responses, and interview questions.

Advanced array methods make it easier to clean, transform, and manage data without writing complex logic.

Understanding flat()

The flat() method is used to remove nesting from arrays. If your array contains other arrays inside it, this method helps you convert everything into a single-level structure.

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

console.log(arr.flat()); // [1, 2, 3, 4, 5, 6]

By default, it removes one level of nesting, but you can control the depth.

let arr = [1, [2, [3, [4]]]];

console.log(arr.flat(2));

Understanding flatMap()

flatMap() combines two steps into one. It first transforms each element using map(), and then flattens the result.

let numbers = [1, 2, 3];

let result = numbers.flatMap((num) => [num, num * 2]);

console.log(result);

This is useful when each item returns multiple values and you want everything in a single array.

Finding Position with findIndex()

findIndex() returns the position of the first element that matches a condition. If no match is found, it returns -1.

let numbers = [5, 10, 15, 20];

let index = numbers.findIndex((num) => num > 12);

console.log(index); // 2

Working with fill()

fill() replaces values in an array with a fixed value. One important thing to remember is that it modifies the original array.

let arr = [1, 2, 3, 4];

arr.fill(0);

console.log(arr);

Using copyWithin()

copyWithin() copies a part of an array and places it somewhere else within the same array. It does not change the size of the array.

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

arr.copyWithin(0, 3);

console.log(arr);

Creating Arrays with Array.from()

Array.from() is used to create a new array from iterable values like strings or objects.

let str = "Hello";

let chars = Array.from(str);

console.log(chars);

Checking Arrays with Array.isArray()

Array.isArray() is the safest way to check whether a value is actually an array.

console.log(Array.isArray([1, 2, 3])); // true
console.log(Array.isArray("Hello"));    // false

Conclusion

Once you understand these methods, working with arrays becomes much easier and more efficient. They help you write cleaner code and handle real-world data without unnecessary complexity.

JavaScript Arrays Array Methods JavaScript flat() JS Interview Questions Advanced JavaScript

More In Javascript

Explore more Javascript articles

Read more articles from the same category or open the full category archive directly.