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.
Return all elements in an array except for the first one
Return Array.prototype.slice(1) if Array.prototype.length is more than 1, otherwise, return the whole array.
JavaScript Objects Explained
JavaScript objects are variables that contain multiple data values. The values within a JS object are known as properties. Objects use keys to name values, much like how is done with variables.
Paste an image from the clipboard
Here’s how to paste an image from your computer’s clipboard using JavaScript.
Highlight an element when dragging a file over it
We will highlight the element when user drags a file over it. For example, the element will have a dashed border which can be simulated by a CSS class.
Get the computed style of an element
Get the actual computed style of an element. This factors in browser default stylesheets as well as external styles you’ve specified.










