DEV Community

Vicky Shinde
Vicky Shinde

Posted on • Edited on

Array methods in JavaScript - Code Vicky

JavaScript provides several built-in methods for working with arrays. Here are some commonly used array methods:

  • push(): Adds one or more elements to the end of an array and returns the new length of the array.
const fruits = ['apple', 'banana'];
fruits.push('orange', 'grape');

console.log(fruits);
// fruits is now ['apple', 'banana', 'orange', 'grape']
Enter fullscreen mode Exit fullscreen mode
  • pop(): Removes the last element from an array and returns that element.
const fruits = ['apple', 'banana', 'orange'];
const removedFruit = fruits.pop();

console.log(fruits)
// removedFruit is 'orange', fruits is now ['apple', 'banana']
Enter fullscreen mode Exit fullscreen mode
  • shift(): Removes the first element from an array and returns that element.
const fruits = ['apple', 'banana', 'orange'];
const removedFruit = fruits.shift();
// removedFruit is 'apple', fruits is now ['banana', 'orange']
console.log(fruits);

Enter fullscreen mode Exit fullscreen mode
  • unshift(): Adds one or more elements to the beginning of an array and returns the new length of the array.
const fruits = ['apple', 'banana', 'orange'];
const removedFruit = fruits.shift();
console.log(fruits);
// removedFruit is 'apple', fruits is now ['banana', 'orange']

Enter fullscreen mode Exit fullscreen mode
  • indexOf(): Returns the first index at which a given element can be found in the array, or -1 if it is not present.
const fruits = ['apple', 'banana', 'orange'];
const index = fruits.indexOf('banana');
console.log(index);
// index is 1
Enter fullscreen mode Exit fullscreen mode
  • splice(): Changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
const fruits = ['apple', 'banana', 'orange'];
fruits.splice(1, 1, 'grape', 'kiwi');
console.log(fruits);
// fruits is now ['apple', 'grape', 'kiwi', 'orange']
Enter fullscreen mode Exit fullscreen mode
  • slice(): Returns a shallow copy of a portion of an array into a new array.
const fruits = ['apple', 'banana', 'orange', 'grape'];
const slicedFruits = fruits.slice(1, 3);
console.log(slicedFruits);
// slicedFruits is ['banana', 'orange'], fruits remains unchanged
Enter fullscreen mode Exit fullscreen mode
  • forEach(): Executes a provided function once for each array element.
const fruits = ['apple', 'banana', 'orange'];
fruits.forEach((fruit) => {
    console.log(fruit);
});
console.log(fruits);
// Outputs: apple, banana, orange
Enter fullscreen mode Exit fullscreen mode

These are just a few examples, and JavaScript provides many more array methods for various operations. Understanding these methods can make it easier to work with arrays in JavaScript.

html

html
Enter fullscreen mode Exit fullscreen mode

I Bulid FREE Web Portfolio Template for Developer - Terminal Edition

Developer Portfolio Template (Next.js) โ€” Terminal Edition | Free & Pro

Stop using the same gradient-hero portfolio every other developer has. This one looks like the tool you actually live in โ€” a dark, fully responsive portfolio template styled as a real terminal, built with Next.js 16 + TypeScript + Tailwind CSS v4.Choose your version below:๐Ÿ†“ Free version includes: Terminal-style hero with live typing animation About, skills, projects, and experience sections Simple contact section Fully responsive โ€” mobile, tablet, desktop One config file to personalize everything (no touching component code) Clean, beginner-friendly codeFREE - Live Preview โญ Pro version includes everything in Free, plus:Everything in the free version, plus the three things that actually turn visitors into clients: real case studies, a blog for SEO and credibility, and a contact form that works. Case study pages โ€” dedicated /work/[project] detail pages for your best work (role, timeline, problem, approach, result). Just add a project to your data file โ€” the page builds itself automatically Built-in blog โ€” /blog index + post pages, perfect for writing dev.to-style content directly on your own portfolio for SEO Working contact form โ€” validated, opens the visitor's email pre-filled with their message. Easy to upgrade to Resend/Formspree for full automation No watermark, no upsell footer โ€” it's 100% yours Same one-file personalization, more pages to show off your real workPRO - Live Preview Why Pro is worth it:A custom portfolio with case studies, a blog, and a working contact form usually costs hundreds of dollars from a freelancer and takes a week of back-and-forth. With Pro, you get all of that as clean, production-ready code you own outright โ€” customizable in an afternoon, not a week.Tech stack: Next.js 16 ยท TypeScript ยท Tailwind CSS v4 ยท Zero bloated dependenciesnpm install npm run devDeploys instantly to Vercel, Netlify, or any Node.js host.License: Personal and portfolio use. Customize and deploy freely for your own site. Not for resale or redistribution as a template.

favicon codervicky.gumroad.com

Top comments (0)