• 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

Combine two arrays using the spread operator

Combine two arrays using the spread operator

This syntax can also be used instead of pushing values to an array: Source https://medium.com/geekculture/20-javascript-snippets-to-code-like-a-pro-86f5fda5598e

Miscellaneous

Check if the user color scheme preference is dark

Check if the user color scheme preference is dark

Use Window.matchMedia() with the appropriate media query to check the user color scheme preference.

Miscellaneous

Resize an image

Resize an image

Assume that we want to resize an image to a given number of percentages.

Image Effects

Cast Any Value to a Boolean

Cast Any Value to a Boolean

In JavaScript, you can use !! to convert anything to boolean.

Miscellaneous

Remove an attribute from an element

Remove an attribute from an element

Remove an attribute (including data attributes) on an element. This method can also be used to manipulate other types of attributes.

Miscellaneous

Capitalize the first letter of a string

Capitalize the first letter of a string

This snippet capitalizes the first letter of a string.

Miscellaneous

Redirect to a specified URL

Redirect to a specified URL

Use Window.location.href or Window.location.replace() to redirect to url. Pass a second argument to simulate a link click (true – default) or an HTTP redirect (false).

Miscellaneous

Preview an image before uploading it

Preview an image before uploading it

Here’s how to use JavaScript to preview an image before uploading it.

Image Effects

Get an attribute on an element

Get an attribute on an element

Use Element.getAttribute() to get an attribute (including data attributes) on an element.

Miscellaneous

Optional Chaining to Prevent Crashes

Optional Chaining to Prevent Crashes

If you access an undefined property, an error is produced. This is where optional chaining comes to the rescue.

Miscellaneous

  • « Go to Previous Page
  • Go to page 1
  • Interim pages omitted …
  • Go to page 16
  • Go to page 17
  • Go to page 18
  • Go to page 19
  • Go to page 20
  • 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