• 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 / Drop list elements from the right

Drop list elements from the right

Drop list elements from the right

Creates a new array with n elements removed from the right.

  • Use Array.prototype.slice() to remove the specified number of elements from the right.
  • Omit the last argument, n, to use a default value of 1.
const dropRight = (arr, n = 1) => arr.slice(0, -n);

// EXAMPLES
dropRight([1, 2, 3]); // [1, 2]
dropRight([1, 2, 3], 2); // [1]
dropRight([1, 2, 3], 42); // []

Source

https://www.30secondsofcode.org/js/s/drop-right

Miscellaneous

Related Snippets:

  • How To Check If a Variable is a String in JavaScript
  • Determine whether two values are the same value
  • Convert a string to an array of characters
  • How To Append An Object To An Array In JavaScript

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers