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.

  • Pluralize string

    Returns the singular or plural form of the word based on the input number, using an optional dictionary if supplied.

    JavaScript, String · Oct 22, 2020

  • Random alphanumeric string

    Generates a random string with the specified length.

    JavaScript, String · Oct 22, 2020

  • RGB to hex

    Converts the values of RGB components to a hexadecimal color code.

    JavaScript, String · Nov 3, 2020

  • Check if a string contains a substring in JavaScript

    Let's have a look at how to check if a string contains a substring in JavaScript.

    JavaScript, String · Jul 27, 2022

  • Tip: Convert a string to a boolean

    Have you ever tried to convert the string representation of a boolean to an actual boolean value? Here's a simple way to do it.

    JavaScript, String · Sep 14, 2022

  • String ends with substring

    Checks if a given string ends with a substring of another string.

    JavaScript, String · Aug 1, 2022

  • Random element in array

    Gets a random element from an array.

    JavaScript, Array · Oct 22, 2020

  • String starts with substring

    Checks if a given string starts with a substring of another string.

    JavaScript, String · Jul 31, 2022

  • Tip: Prevent a string from being escaped in JavaScript

    Strings in JavaScript can be escaped in various ways. But what if you need to prevent a string from being escaped? Here's a handy trick for that.

    JavaScript, String · Jun 17, 2021

  • Encode string to Base64

    Creates a base-64 encoded ASCII string from a String object in which each character in the string is treated as a byte of binary data.

    JavaScript, Node · Sep 15, 2020

  • Colorize text

    Adds special characters to text to print in color in the console (combined with console.log()).

    JavaScript, Node · Nov 3, 2020

  • Case-insensitive substring search

    Checks if a string contains a substring, case-insensitive.

    JavaScript, String · Jul 28, 2022

  • Check if absolute URL

    Checks if the given string is an absolute URL.

    JavaScript, String · Oct 20, 2020

  • Remove accents

    Removes accents from strings.

    JavaScript, String · Oct 22, 2020

  • String to slug

    Converts a string to a URL-friendly slug.

    JavaScript, String · Oct 4, 2020