Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to Remove Multiple Elements from Array in JavaScript?
Next article icon

How to Remove First and Last Element from Array using JavaScript?

Last Updated : 17 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Removing the first and last elements from an array is a common operation. This can be useful in various scenarios, such as data processing, filtering, or manipulation.

Example:

Input: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
Output: [ 2, 3, 4, 5, 6, 7, 8, 9 ]

Removing first and last element in array can be done using below approaches:

Table of Content

  • Using Slice() Method
  • Using Splice() Method

Using Slice() Method

The approach involves creating a new array by extracting a portion of an existing array. The slice() method is used to take elements starting from the second position up to the second-to-last position. This excludes the first and last elements of the original array. The resulting new array contains all the elements from the second to the penultimate one of the original array.

Example: This example shows that the slice() method can be used to remove the first and last elements from an array.

JavaScript
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; let newArray = arr.slice(1, -1); console.log(newArray);  

Output
[   2, 3, 4, 5,   6, 7, 8, 9 ] 

Using Splice() Method

The approach involves populating an array with a sequence of numbers and then removing the first and last elements if the array contains more than one element. The array is initially empty and numbers from 0 to 9 are added to it. After ensuring the array has more than one element, the splice() method is used to remove the first element and then the last element. The resulting array excludes the original first and last elements.

Example: This example shows that the splice() method can be used to remove elements from an array.

JavaScript
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; if (arr.length > 1) {     arr.splice(0, 1);     arr.splice(arr.length - 1, 1); } console.log(arr);  

Output
[   2, 3, 4, 5,   6, 7, 8, 9 ] 

Next Article
How to Remove Multiple Elements from Array in JavaScript?

C

chotumishraq3v1
Improve
Article Tags :
  • JavaScript
  • Web Technologies

Similar Reads

  • JavaScript - How to Remove an Element from an Array?
    Removing elements from an array is a fundamental operation in JavaScript, essential for data manipulation, filtering, and transformation. This guide will explore different methods to efficiently remove elements from an array, enhancing your understanding and capability in handling arrays. 1. Using p
    3 min read
  • JavaScript - How to Get First N Elements from an Array?
    There are different ways to get the first N elements from array in JavaScript. Examples: Input:arr = [1, 2, 3, 4, 5, 6], n = 3 Output: [1, 2, 3] Input:arr = [6, 1, 4, 9, 3, 5, 7], n = 4Output: [6, 1, 4, 9]1. Using slice() MethodThe slice() method is used to extract a part of an array and returns a n
    4 min read
  • How to Remove Multiple Elements from Array in JavaScript?
    Here are various methods to remove multiple elements from an array in JavaScript 1. Using filter() MethodThe filter() method creates a new array with the elements that pass the condition. This method does not change the original array. [GFGTABS] JavaScript let a = [1, 2, 3, 4, 5]; let remove = [2, 4
    4 min read
  • Remove Elements From a JavaScript Array
    Here are the various methods to remove elements from a JavaScript Array 1. Using pop() methodThe pop() method removes and returns the last element of an array. This function decreases the length of the array by 1 every time the element is removed. [GFGTABS] javascript let a = ["Apple",
    6 min read
  • How to delete an element from an array using JavaScript ?
    The array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index. In this article, we will discuss different ways to remove elements from the array.
    5 min read
  • How to remove falsy values from an array in JavaScript ?
    Falsy/Falsey Values: In JavaScript, there are 7 falsy values, which are given below falsezero(0,-0)empty string("", ' ' , ` `)BigIntZero(0n,0x0n)nullundefinedNaNIn JavaScript, the array accepts all types of falsy values. Let's see some approaches on how we can remove falsy values from an array in Ja
    6 min read
  • Remove Empty Elements from an Array in JavaScript
    Here are different approaches to remove empty elements from an Array in JavaScript. 1. Using array.filter() MethodThe array filter() method is used to create a new array from a given array consisting of elements that satisfy given conditions. array.filter( callback( element, index, arr ), thisValue
    3 min read
  • Find the min/max element of an Array using JavaScript
    To find the minimum or maximum element in a JavaScript array, use Math.min or Math.max with the spread operator. JavaScript offers several methods to achieve this, each with its advantages. Using Math.min() and Math.max() Methods The Math object's Math.min() and Math.max() methods are static methods
    2 min read
  • How to Remove duplicate elements from array in JavaScript ?
    In this article, we will discuss the methods to remove duplicate elements from a Javascript array. There are various methods to remove duplicates in the array. These are the following ways: Table of Content Using filter() MethodUsing set() MethodUsing forEach() MethodUsing reduce() MethodUsing index
    4 min read
  • How to remove specific elements from the left of a given array of elements using JavaScript ?
    In this article, we will learn How to remove specific elements from the left of a given array of elements using JavaScript. We have given an array of elements, and we have to remove specific elements from the left of a given array. Here are some common approaches: Table of Content Using splice() met
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences