JavaScript 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.

  • Midpoint

    Calculates the midpoint between two pairs of (x,y) points.

    JavaScript, Math · Oct 21, 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

  • Remove DOM element

    Removes an element from the DOM.

    JavaScript, Browser · Jan 6, 2021

  • RGB to HSB

    Converts a RGB color tuple to HSB format.

    JavaScript, Math · Oct 22, 2020

  • RGB to HSL

    Converts a RGB color tuple to HSL format.

    JavaScript, Math · Oct 4, 2020

  • Array tail

    Returns all elements in an array except for the first one.

    JavaScript, Array · Oct 22, 2020

  • Truncate string at whitespace

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

    JavaScript, String · Oct 21, 2020

  • How can I detect an undefined object property in JavaScript?

    Learn how to detect undefined object properties in JavaScript the correct way.

    JavaScript, Object · Aug 7, 2022

  • Attempt invoking a function

    Attempts to invoke a function with the provided arguments, returning either the result or the caught error object.

    JavaScript, Function · Oct 18, 2020

  • Detect device type

    Detects whether the page is being viewed on a mobile device or a desktop.

    JavaScript, Browser · Oct 22, 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

  • Value is object

    Checks if the passed value is an object or not.

    JavaScript, Type · Jan 7, 2021

  • Value is plain object

    Checks if the provided value is an object created by the Object constructor.

    JavaScript, Type · Oct 20, 2020