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
Check if the user color scheme preference is dark
Use Window.matchMedia() with the appropriate media query to check the user color scheme preference.
Resize an image
Assume that we want to resize an image to a given number of percentages.
Cast Any Value to a Boolean
In JavaScript, you can use !! to convert anything to boolean.
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.
Capitalize the first letter of a string
This snippet capitalizes the first letter of a string.
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).
Preview an image before uploading it
Here’s how to use JavaScript to preview an image before uploading it.
Get an attribute on an element
Use Element.getAttribute() to get an attribute (including data attributes) on an element.
Optional Chaining to Prevent Crashes
If you access an undefined property, an error is produced. This is where optional chaining comes to the rescue.