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.

  • Byte size of string

    Returns the length of a string in bytes.

    JavaScript, String · Oct 18, 2020

  • String is alpha

    Checks if a string contains only alpha characters.

    JavaScript, String · Dec 31, 2020

  • Mask a value

    Replaces all but the last num of characters with the specified mask character.

    JavaScript, String · Oct 21, 2020

  • Serialize cookie

    Serializes a cookie name-value pair into a Set-Cookie header string.

    JavaScript, Browser · Oct 22, 2020

  • Are JavaScript strings immutable?

    When it comes to immutability, many developers have trouble wrapping their head around JavaScript strings. Yet they're not as complicated as you might expect.

    JavaScript, String · Oct 10, 2021

  • Decode Base64 encoded string

    Decodes a string of data which has been encoded using base-64 encoding.

    JavaScript, Node · Sep 15, 2020

  • Check if string contains whitespace

    Checks if the given string contains any whitespace characters.

    JavaScript, String · Oct 18, 2020

  • Pad string

    Pads a string on both sides with the specified character, if it's shorter than the specified length.

    JavaScript, String · Oct 22, 2020

  • Truncate string at whitespace

    Truncates a string up to specified length, respecting whitespace when possible.

    JavaScript, String · Oct 21, 2020

  • Format number

    Formats a number using the local number format order.

    JavaScript, String · Oct 22, 2020

  • String from camelcase

    Converts a string from camelcase.

    JavaScript, String · Oct 22, 2020

  • Indent string

    Indents each line in the provided string.

    JavaScript, String · Nov 1, 2020

  • Number to currency string

    Takes a number and returns it in the specified currency formatting.

    JavaScript, Math · Oct 22, 2020

  • Truncate string

    Truncates a string up to a specified length.

    JavaScript, String · Oct 21, 2020

  • Value is string

    Checks if the given argument is a string. Only works for string primitives.

    JavaScript, Type · Oct 20, 2020