DEV Community

Cover image for Pick Up The Power Of Array Method (Part 1)
Crosston J
Crosston J

Posted on

Pick Up The Power Of Array Method (Part 1)

INTRODUCTION :

  • Arrays play a big role in JavaScript. From storing data to updating it, array methods help us manage and work with values easily.

  • In this part, we’ll look at the most commonly used array methods — simple to learn, but powerful in real-world coding. Each method is explained clearly, with examples you can try right away.

  • This guide is great for beginners or anyone who wants to revise the basics.

Let’s get started...

1.length()

  • Gives back the array's total number of elements.
const arr = [10, 20, 30]; console.log(arr.length);  // 3
Enter fullscreen mode Exit fullscreen mode

2.toString()

  • creates a string from an array by separating its elements with commas.
const arr = [1, 2, 3]; 
console.log(arr.toString());  // "1,2,3"
Enter fullscreen mode Exit fullscreen mode

3. at()

  • Gives back the element at the given index. allows for negative indexing.
const arr = [10, 20, 30, 40];
console.log(arr.at(2)); // 30
console.log(arr.at(-1)); // 40
Enter fullscreen mode Exit fullscreen mode

4. Use join()

-Uses a custom separator to join array elements into a single string.

const arr = ["HTML", "CSS", "JS"];

console.log(arr.join(" - "));

 // "HTML - CSS - JS"
Enter fullscreen mode Exit fullscreen mode

5. pop()

  • Returns the array's final element after removing it.
const arr = [1, 2, 3]; console.log(arr.pop());

 // 3 console.log(arr); // [1, 2]
Enter fullscreen mode Exit fullscreen mode

6. push()

  • Adds one or more elements to the end of the array and returns the new length.
const arr = [1, 2, 3];
console.log(arr.pop());  // 3
console.log(arr);        // [1, 2]

Enter fullscreen mode Exit fullscreen mode

7. shift()

  • returns the array's first element after removing it.
  • Its remove the first element
const arr = [10, 20, 30]; console.log(arr.shift()); // 10 console.log(arr); // [20, 30]
Enter fullscreen mode Exit fullscreen mode

8. unshift()

  • Returns the new length after adding elements to the array's beginning.
const arr = [20, 30]; console.log(arr); 
arr.unshift(10); // [10, 20, 30]

Enter fullscreen mode Exit fullscreen mode

9. delete()

  • removes an element from a given index, while leaving it undefined.
  • Note: Be careful when using. The array length remains unchanged.
const arr = [1, 2, 3]; 
delete arr[1]; 
console.log(arr); // [1, undefined, 3]
Enter fullscreen mode Exit fullscreen mode

✦ Final Note

Now that we’ve explored some of the basic array methods, we’ve built a solid foundation. But this is just the beginning — there are still more powerful methods waiting to be uncovered.

In Part 2, we’ll dive into more powerful methods like splice(), toSpliced(), flat(), and more — with real examples and clear explanations.
So stay tuned, and let’s continue this learning journey together!

Quotes time :

"Every method you learn today is a tool you’ll wield tomorrow."

Top comments (0)