DEV Community

Cover image for Array Without Last Element | Programming Tutorials | Lab
Labby for LabEx

Posted on

Array Without Last Element | Programming Tutorials | Lab

Introduction

This article covers the following tech skills:

Skills Graph

In this lab, we will explore how to manipulate arrays in JavaScript by creating a function that returns all the elements of an array except the last one. We will use the Array.prototype.slice() method to achieve this and learn how to slice and extract elements from arrays. This lab will help us understand the fundamentals of working with arrays in JavaScript.

How to Get an Array Without the Last Element

To practice coding, open the Terminal/SSH and type node. Here's how you can return all the elements of an array except the last one:

  • Use Array.prototype.slice() to return all the elements of the array except the last one.
const initial = (arr) => arr.slice(0, -1);
Enter fullscreen mode Exit fullscreen mode

Here's an example:

initial([1, 2, 3]); // [1, 2]
Enter fullscreen mode Exit fullscreen mode

Summary

Congratulations! You have completed the Array Without Last Element lab. You can practice more labs in LabEx to improve your skills.

MindMap


🚀 Practice Now: Array Without Last Element


Want to Learn More?

Top comments (0)