JavaScript String Snippets

The JavaScript snippet collection contains a wide variety of ES6 helper functions. It includes helpers for dealing with primitives, arrays and objects, as well as algorithms, DOM manipulation functions and Node.js utilities.

  • Learn how to use the JavaScript ES6 spread syntax to converting iterables to arrays and level up your code today.

  • Format duration

    JavaScript, Date

    Returns the human-readable format of the given number of milliseconds.

  • URL parameters as object

    JavaScript, Browser

    Creates an object containing the parameters of the current URL.

  • JSON to CSV

    JavaScript, Array

    Converts an array of objects to a comma-separated values (CSV) string that contains only the columns specified.

  • CSV to JSON

    JavaScript, String

    Converts a comma-separated values (CSV) string to a 2D array of objects. The first row of the string is used as the title row.

  • Index of substrings

    JavaScript, String

    Finds all the indexes of a substring in a given string.

  • HSL to object

    JavaScript, String

    Converts an hsl() color string to an object with the values of each color.

  • RGB to object

    JavaScript, String

    Converts an rgb() color string to an object with the values of each color.

  • Common regular expressions

    JavaScript, String

    A collection of regular expressions that can be used to solve common problems.

  • Change color lightness

    JavaScript, String

    Changes the lightness value of an hsl() color string.

  • Parse cookie

    JavaScript, Browser

    Parses an HTTP Cookie header string, returning an object of all cookie name-value pairs.

  • Caesar cipher

    JavaScript, Algorithm

    Encrypts or decrypts a given string using the Caesar cipher.

  • Serialize form

    JavaScript, Browser

    Encodes a set of form elements as a query string.

  • Email address validation can be much trickier than it sounds. Here's why and my advice on how to approach this problem.

  • Copy to clipboard

    JavaScript, Browser

    Copies a string to the clipboard. Only works as a result of user action (i.e. inside a click event listener).