Skip to main content

JavaScript function to remove the first very first character in a string, if it exists... otherwise return the original string (untouched).

/**
 * Strips the specified character (charToRemove) from the string (str) if
 * it is positioned as the first character in the string (str).
 *
 * @param {string} str The string containing the potential character to strip/remove.
 * @param {string} charToRemove The character to remove from the beginning of the string.
 * @returns {string} The original string with the leading character stripped, otherwise the original string.
 */
function stripLeadingChar(str, charToRemove) {
    return str.charAt(0) === charToRemove ? str.slice(1) : str;
}