• 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

Sorting Objects with a Primary and Secondary Property

Sorting Objects with a Primary and Secondary Property

When you sort an array and have several matches exact matches, it is handy to have a secondary property to sort by to break the tie. You can use the same approach as above, but with a little more logic. Inside the compareFunction, if the two properties have the same value (a zero compare value), then […]

Miscellaneous

Find a specific object in an array of objects

Find a specific object in an array of objects

This is arguably one of the most common tasks you’ll need to accomplish in the JS world. Iterating through an array of objects to find a specific one. The find method is our friend here. Simply plugin the selection criteria using an anonymous function as the argument and you’re set: Source https://levelup.gitconnected.com/6-javascript-code-snippets-for-solving-common-problems-33deb6cacef3

Miscellaneous

Check if all array elements are unique

Check if all array elements are unique

Checks if all elements in an array are unique. Create a new Set from the mapped values to keep only unique occurrences. Use Array.prototype.length and Set.prototype.size to compare the length of the unique values to the original array. Source https://www.30secondsofcode.org/js/s/all-unique

Miscellaneous

Insert an element before another one

Insert an element before another one

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

Miscellaneous

Deep clone a value in Node.js

Deep clone a value in Node.js

If you want to deep clone a value in Node.js, you no longer need to use a library or the JSON.parse(JSON.stringify(value)) hack. You can use the new global function structuredClone() Source https://github.com/roeib/JavaScript-snippets#structuredclone

Miscellaneous

Add an element to the DOM

The original way to add elements to the DOM. Call it on the parent of the element you’re inserting your new element before (the referenceNode), and pass in both the new element and the reference node as arguments. You can also use it to inject an element after another one by using the .nextSibling property on your referenceNode. Source https://vanillajstoolkit.com/reference/dom-injection/element-insertbefore/

Miscellaneous

Remove falsy values from an array

Remove falsy values from an array

Removes falsy values from an array. Use Array.prototype.filter() to filter out falsy values (false, null, 0, “”, undefined, and NaN). Source https://www.30secondsofcode.org/js/s/compact

Miscellaneous

How to change the value of an object which is inside an array

How to change the value of an object which is inside an array

Source https://github.com/roeib/JavaScript-snippets#how-to-change-the-value-of-an-object-which-is-inside-an-array

Miscellaneous

Create an element

Create an element

Source https://vanillajstoolkit.com/reference/dom-injection/createelement/

Miscellaneous

Date difference in months

Date difference in months

Calculates the difference (in months) between two dates. Use Date.prototype.getFullYear() and Date.prototype.getMonth() to calculate the difference (in months) between two Date objects. Source https://www.30secondsofcode.org/js/s/get-months-diff-between-dates

Time & Date

  • « Go to Previous Page
  • Go to page 1
  • Interim pages omitted …
  • Go to page 5
  • Go to page 6
  • Go to page 7
  • Go to page 8
  • Go to page 9
  • 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