• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Miscellaneous / Return all the elements of an array except the last one

Return all the elements of an array except the last one

Return all the elements of an array except the last one

Use Array.prototype.slice(0, -1) to return all but the last element of the array.

const initial = arr => arr.slice(0, -1);

// Example
initial([1, 2, 3]); // [1, 2]

Source

https://www.30secondsofcode.org/js/s/initial

Miscellaneous

Related Snippets:

  • Convert a string to a number
  • Copy text to the clipboard
  • Remove a Character From a String in JavaScript
  • Add characters to the end of a string if it’s less than a certain length

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2022 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us