• 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

Assign keys to an object with the same name

Assign keys to an object with the same name

When you are assigning keys to an object, if the key is the same name as the variable that holds the value you want to assign you can omit the value assignment altogether. This prevents you from having to repeat yourself, something we all hate doing. Let’s take a look at an example using some […]

Miscellaneous

Drop list elements from the right

Drop list elements from the right

Creates a new array with n elements removed from the right. Source https://www.30secondsofcode.org/js/s/drop-right

Miscellaneous

Insert an element at the end of a set of elements inside a shared parent

Insert an element at the end of a set of elements inside a shared parent

Call the append() method on the reference node, and pass in the new node as an argument. Source https://vanillajstoolkit.com/reference/dom-injection/element-append/

Miscellaneous

Destructing variable assignment

Destructing variable assignment

Assigning variables from an array one-by-one is time consuming and silly. Just use destructuring assignment to accomplish this quicker and easier: Source https://levelup.gitconnected.com/6-javascript-code-snippets-for-solving-common-problems-33deb6cacef3

Miscellaneous

Find last n matches

Find last n matches

Finds the last n elements for which the provided function returns a truthy value. Use a for loop to execute the provided matcher for each element of arr. Use Array.prototype.unshift() to prepend elements to the results array and return them if its length is equal to n. Source https://www.30secondsofcode.org/js/s/find-last-n

Miscellaneous

Insert an element at the beginning of a set elements inside a shared parent

Insert an element at the beginning of a set elements inside a shared parent

Call the prepend() method on the reference node, and pass in the new node as an argument. Source https://vanillajstoolkit.com/reference/dom-injection/element-prepend/

Miscellaneous

Filtering an array of objects based on a condition

Filtering an array of objects based on a condition

If you have a large array of data and want to filter out items based on a specific condition then you can simply use the filter function. In this example we have an array of file paths. Some files are in ‘dir1’ while others are in ‘dir2’. Let’s say we want to filter for only a specific directory: Filtering for […]

Miscellaneous

Check if HTML element has class

Check if HTML element has class

Use Element.classList and DOMTokenList.contains() to check if the element has the specified class. Source https://www.30secondsofcode.org/js/s/has-class

CSS

Insert an element into the DOM after another one

Insert an element into the DOM after another one

Insert an element in the DOM after another one. Call the after() method on the reference node, and pass in the new node as an argument. Source https://vanillajstoolkit.com/reference/dom-injection/element-after/

Miscellaneous

Loop over an object’s keys and values

Loop over an object’s keys and values

Sometimes your data structure might be a complex object that contains a bunch of key/value pairs. Iterating over each pair is a little odd at first glance depending on what languages you’re used to but its straightforward once you get used to using the functions of Object. After you grab the objects keys you can loop […]

Miscellaneous

  • « Go to Previous Page
  • Go to page 1
  • Interim pages omitted …
  • Go to page 4
  • Go to page 5
  • Go to page 6
  • Go to page 7
  • Go to page 8
  • Interim pages omitted …
  • Go to page 36
  • Go to Next Page »

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