30 seconds of code Short code snippets for all your development needs https://www.30secondsofcode.org en-us https://www.30secondsofcode.org/assets/logo.png 30 seconds of code https://www.30secondsofcode.org 1440 Convert a JSON object with circular references to a JavaScript string https://www.30secondsofcode.org/js/s/stringify-circular-json Circular JSON objects can't be serialized using JSON.stringify(), but you can use this trick to handle them. Mon, 18 Mar 2024 00:00:00 GMT Check if a JavaScript string is a valid JSON https://www.30secondsofcode.org/js/s/string-is-valid-json Use a simple JavaScript trick to validate a serialized JSON object. Sun, 17 Mar 2024 00:00:00 GMT Open all links in an HTML document in a new tab https://www.30secondsofcode.org/html/s/open-all-links-in-new-tab Using an HTML element, you can easily make all links in your document open in a new tab. Learn how with this quick tip. Sat, 16 Mar 2024 00:00:00 GMT Sampling, shuffling and weighted selection in JavaScript arrays https://www.30secondsofcode.org/js/s/array-sample-shuffle-weighted-selection Learn how to shuffle, sample and perform weighted selection on JavaScript arrays. Fri, 15 Mar 2024 00:00:00 GMT Generate a random alphanumeric JavaScript string https://www.30secondsofcode.org/js/s/random-alphanumeric Use JavaScript to generate a random alphanumeric string of a specified length. Fri, 15 Mar 2024 00:00:00 GMT Generate a JavaScript array of random integers in a given range https://www.30secondsofcode.org/js/s/random-integer-array-in-range Combining Math.random() and a few simple math operations, you can generate an array of random integers in a specified range. Fri, 15 Mar 2024 00:00:00 GMT Strip HTML tags using JavaScript https://www.30secondsofcode.org/js/s/strip-html-tags Use a regular expression to remove HTML/XML tags from a string. Fri, 15 Mar 2024 00:00:00 GMT Colorize text in the Node.js terminal https://www.30secondsofcode.org/js/s/color-console-output Use special Unicode characters to print text in color in the console. Thu, 14 Mar 2024 00:00:00 GMT Generate a random boolean value in JavaScript https://www.30secondsofcode.org/js/s/random-boolean A quick, one-liner to generate a random boolean value in JavaScript. Thu, 14 Mar 2024 00:00:00 GMT Check if a JavaScript date is inside business hours https://www.30secondsofcode.org/js/s/date-inside-business-hours Leverage the Date object to check if a given date is inside business hours. Wed, 13 Mar 2024 00:00:00 GMT Generate a random number or integer in a range with JavaScript https://www.30secondsofcode.org/js/s/random-number-or-integer-in-range Enhance JavaScript's Math.random() to generate a random number or integer in a specified range. Tue, 12 Mar 2024 00:00:00 GMT Convert an integer to a roman numeral in JavaScript https://www.30secondsofcode.org/js/s/to-roman-numeral Roman numerals are often used for stylistic reasons, but converting an integer to a roman numeral can be a bit tricky. Tue, 12 Mar 2024 00:00:00 GMT Calculate the powerset of a JavaScript array https://www.30secondsofcode.org/js/s/powerset Generate the powerset of a given array of numbers or other primitive values. Mon, 11 Mar 2024 00:00:00 GMT Find all indexes of a substring in a JavaScript string https://www.30secondsofcode.org/js/s/index-of-substrings Create a generator function that finds all the indexes of a substring in a given string. Sun, 10 Mar 2024 00:00:00 GMT Calculate the byte size of a JavaScript string https://www.30secondsofcode.org/js/s/byte-size Figure out the exact number of bytes in a JavaScript string, including Unicode characters. Sat, 09 Mar 2024 00:00:00 GMT Create a directory if it doesn't exist using Node.js https://www.30secondsofcode.org/js/s/create-directory-if-not-exists Learn how to create a directory using Node.js, if it doesn't exist. Fri, 08 Mar 2024 00:00:00 GMT Convert an HSL color string to a JavaScript array or object https://www.30secondsofcode.org/js/s/hsl-to-array-or-object Easily convert an hsl() color string to an array of values or an object with the values of each color. Thu, 07 Mar 2024 00:00:00 GMT Convert an RGB color string to a JavaScript array or object https://www.30secondsofcode.org/js/s/rgb-to-array-or-object Easily convert an rgb() color string to an array of values or an object with the values of each color. Wed, 06 Mar 2024 00:00:00 GMT How can I convert between hexadecimal, RGB, HSL and HSB color formats in JavaScript? https://www.30secondsofcode.org/js/s/rgb-hex-hsl-hsb-color-format-conversion Learn how to easily convert between the various color formats, using JavaScript and a few simple formulas. Tue, 05 Mar 2024 00:00:00 GMT Check if two JavaScript arrays have same contents https://www.30secondsofcode.org/js/s/arrays-have-same-contents Learn how to compare the contents of two arrays to see if they contain the same elements regardless of order. Mon, 04 Mar 2024 00:00:00 GMT Implement the SDBM hash function in JavaScript https://www.30secondsofcode.org/js/s/hash-string-into-number SDBM is a simple, non-cryptographic hash function that can hash strings into whole numbers. Here's a JavaScript implementation. Sun, 03 Mar 2024 00:00:00 GMT Use JavaScript to listen and handle scroll stop events https://www.30secondsofcode.org/js/s/on-scroll-stop Use some clever JavaScript tricks to listen for and handle scroll stop events in the browser. Sat, 02 Mar 2024 00:00:00 GMT Define a system font stack in CSS https://www.30secondsofcode.org/css/s/system-font-stack Learn how to leverage the native fonts of the operating system to get close to a native app feel. Fri, 01 Mar 2024 00:00:00 GMT Word wrap a JavaScrip string https://www.30secondsofcode.org/js/s/word-wrap Wrap a string to a given number of characters using a string break character in JavaScript. Thu, 29 Feb 2024 00:00:00 GMT Create a JavaScript promise that resolves after a given amount of time https://www.30secondsofcode.org/js/s/resolve-promise-after-amount-of-time Learn how to create a promise that resolves after a given amount of time in JavaScript. Wed, 28 Feb 2024 00:00:00 GMT Apply a mapping function to each character in a JavaScript string https://www.30secondsofcode.org/js/s/map-string Learn how to replicate the behavior of Array.prototype.map() for strings. Tue, 27 Feb 2024 00:00:00 GMT Find the number of days in a month using JavaScript https://www.30secondsofcode.org/js/s/days-in-month Calculate the number of days in a month for a given year using JavaScript. Mon, 26 Feb 2024 00:00:00 GMT Get the scroll position of the current page using JavaScript https://www.30secondsofcode.org/js/s/get-scroll-position Calculate the coordinates of the current scroll position in the browser window using JavaScript. Sun, 25 Feb 2024 00:00:00 GMT Get the width of the scrollbar using JavaScript https://www.30secondsofcode.org/js/s/get-scrollbar-width Easily and reliably calculate the width of the browser's vertical scrollbar with JavaScript. Sat, 24 Feb 2024 00:00:00 GMT Find the prime factors of a number in JavaScript https://www.30secondsofcode.org/js/s/prime-factors Find the prime factors of a number using trial division in JavaScript. Fri, 23 Feb 2024 00:00:00 GMT Implementing the Luhn Algorithm in JavaScript https://www.30secondsofcode.org/js/s/luhn-check Implement the Luhn Algorithm, used to validate a variety of identification numbers. Thu, 22 Feb 2024 00:00:00 GMT Get, set or add styles to an HTML element with JavaScript https://www.30secondsofcode.org/js/s/get-set-add-styles-to-html-element Learn how to retrieve and manipulate the styles of an HTML element easily and efficiently with JavaScript. Wed, 21 Feb 2024 00:00:00 GMT JavaScript function methods - call(), apply() and bind() https://www.30secondsofcode.org/js/s/function-call-apply-bind Learn everything you need to know about JavaScript's call(), apply() and bind() in this short guide. Tue, 20 Feb 2024 00:00:00 GMT Transform the keys of a JavaScript object https://www.30secondsofcode.org/js/s/transform-object-keys Learn how to perform various transformations on the keys of a JavaScript object. Mon, 19 Feb 2024 00:00:00 GMT Escape or unescape HTML using JavaScript https://www.30secondsofcode.org/js/s/escape-unescape-html Learn how to convert text into HTML-safe strings and vice versa using JavaScript. Sun, 18 Feb 2024 00:00:00 GMT Get the first or last date of a month using JavaScript https://www.30secondsofcode.org/js/s/first-last-date-of-month Use the Date object and some clever tricks to get the first or last date of a month in JavaScript. Sat, 17 Feb 2024 00:00:00 GMT Calculate the nth root of a number in JavaScript https://www.30secondsofcode.org/js/s/nth-number-root Use Math.pow() to calculate the nth root of a given number in JavaScript. Sat, 17 Feb 2024 00:00:00 GMT Check if all values of a JavaScript array are equal https://www.30secondsofcode.org/js/s/all-values-of-array-are-equal Use the Array.prototype.every() method to check if all values of an array are equal in JavaScript. Fri, 16 Feb 2024 00:00:00 GMT Arithmetic and geometric progression in JavaScript https://www.30secondsofcode.org/js/s/arithmetic-and-geometric-progression Create arrays of numbers in arithmetic and geometric progression. Fri, 16 Feb 2024 00:00:00 GMT Calculate the ranking of a JavaScript array https://www.30secondsofcode.org/js/s/array-ranking Calculate the ranking of an array based on a comparator function in JavaScript. Fri, 16 Feb 2024 00:00:00 GMT Check if a numeric array is sorted in JavaScript https://www.30secondsofcode.org/js/s/array-is-sorted Efficiently check if a numeric array is sorted in ascending or descending order in JavaScript. Thu, 15 Feb 2024 00:00:00 GMT Can I freeze a Set or Map in JavaScript? https://www.30secondsofcode.org/js/s/freeze-set-or-map Learn how to create a frozen Set or Map in JavaScript. Thu, 15 Feb 2024 00:00:00 GMT Clamp or map a number to a range in JavaScript https://www.30secondsofcode.org/js/s/clamp-or-map-number-to-range Clamping and mapping a number to a range are two common and easily confused operations. Learn how to perform each in JavaScript. Wed, 14 Feb 2024 00:00:00 GMT Formatting numeric values in JavaScript https://www.30secondsofcode.org/js/s/number-formatting Learn common number formatting operations, such as rounding, padding, optional decimal marks, currency, seconds, bytes, and more. Wed, 14 Feb 2024 00:00:00 GMT How can I perform an HTTP request in JavaScript? https://www.30secondsofcode.org/js/s/http-get-post-put-delete Learn how to perform HTTP GET, POST, PUT, and DELETE requests in JavaScript. Tue, 13 Feb 2024 00:00:00 GMT Mask a JavaScript value https://www.30secondsofcode.org/js/s/mask-string Replace all characters in a string, except for the ending characters, with a mask character. Mon, 12 Feb 2024 00:00:00 GMT How can I convert a Map to a JavaScript object and vice versa? https://www.30secondsofcode.org/js/s/convert-map-to-object Learn how to convert between Maps and objects in JavaScript. Sun, 11 Feb 2024 00:00:00 GMT Check if an element is visible in the viewport using JavaScript https://www.30secondsofcode.org/js/s/element-is-visible-in-viewport Learn how to check if an element is visible in the browser's viewport, using this simple technique. Sun, 11 Feb 2024 00:00:00 GMT Perform math operations on arrays of numbers in JavaScript https://www.30secondsofcode.org/js/s/numeric-array-math-operations Learn how to work with arrays of numbers in JavaScript, performing common math operations such as sum, average, product and more. Sun, 11 Feb 2024 00:00:00 GMT How do I use JavaScript to modify the URL without reloading the page? https://www.30secondsofcode.org/js/s/modify-url-without-reload Learn all of the options JavaScript provides for modifying the URL of the current page in the browser without reloading the page. Sat, 10 Feb 2024 00:00:00 GMT