• 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 every nth element in an array

Return every nth element in an array

Return every nth element in an array

Use Array.prototype.filter() to create a new array that contains every nth element of a given array.

const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);

// EXAMPLE
everyNth([1, 2, 3, 4, 5, 6], 2); // [ 2, 4, 6 ]

Source

https://www.30secondsofcode.org/js/s/every-nth

Miscellaneous

Related Snippets:

  • Looping over an object’s keys and values
  • Check if the given string is an absolute URL
  • How to Get the Query String in JavaScript
  • How To Delete a Specific Element From 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