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
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"
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
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"
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]
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]
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]
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]
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]
✦ 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)