Useful Javascript Functions

This is a list of Javascript functions that I find useful for everyday use.

/**
 * Evaluate if one string exists inside another string and return
 * either true (it exists) or false (it does not).
 *
 * @param  string needle   The string to search for.
 * @param  string haystack The string to search through.
 *
 * @return bool            Return true if it exists, false otherwise.
 */
function strContains(needle, haystack) {
    // Make everything lowercase
    needle = needle.toLowerCase();
    haystack = haystack.toLowerCase();
    // See if the needle exists in the haystack
    strLocation = haystack.indexOf(needle);
    if (strLocation != -1) {
        value = true;
    } else {
        value = false;
    }
    // Return the value
    return value;
}
comments powered by Disqus
javascript/useful_javascript_functions.txt · Last modified: 2020/06/01 22:53 (external edit)