Monday, November 17, 2025
HomeLanguagesJavascriptUnderscore.js _.noConflict() Function

Underscore.js _.noConflict() Function

Underscore.js is a library in javascript that makes operations on arrays, string, objects much easier and handy. 
The _.noConflict() function is used to create a reference of the global underscore object “_” to another variable.

Note: It is very necessary to link the underscore CDN before going and using underscore functions in the browser. When linking the underscore.js CDN The “_” is attached to the browser as a global variable.

Syntax:

_.noConflict()

Parameters: This function does not accept any parameter.

Return Value: It returns the reference to the global underscore variable.

Example 1: When noConflict() function is not used and using “underscore” variable.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
 
     
<p>Click the given below button</p>
 
 
    <button>
        button
    </button>
 
    <script>
        let btn = document.querySelector("button");
        let p = document.querySelector("p")
 
        // Creating a array
        let arr = [2, 3, 1, 2, 5, 1];
 
        // Declaring underscore variable
        let underscore;
 
        // Creating a function
        let func = () => {
 
            // Changing text of paragraph
            // on button click
            p.innerText = "button is clicked";
 
            // Sorting the array
            arr = underscore.sortBy(arr,
                (e) => { return Math.round(e) })
            console.log(arr)
        }
        btn.addEventListener("click", func);
    </script>
</body>
 
</html>


Output:

  • When button is not clicked: 
     

  • When button is clicked: 
     

Example 2: When noConflict() function is used.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
 
     
<p>Click the given below button</p>
 
 
    <button>
        button
    </button>
 
    <script>
        let btn = document.querySelector("button");
        let p = document.querySelector("p")
        let arr = [2, 3, 1, 2, 5, 1];
 
        // Using underscore as a reference
        // to global _ variable
        let underscore = _.noConflict();
 
        // Creating a function named func`
        let func = () => {
            p.innerText = "button is clicked";
            arr = underscore.sortBy(arr,
                (e) => { return Math.round(e) })
            console.log(arr)
        }
         
        // Adding event listener to button
        btn.addEventListener("click", func);
    </script>
</body>
 
</html>


Output:

  • When button is not clicked: 
     

  • When button is clicked: 
     

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32402 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6769 POSTS0 COMMENTS
Nicole Veronica
11920 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11990 POSTS0 COMMENTS
Shaida Kate Naidoo
6897 POSTS0 COMMENTS
Ted Musemwa
7150 POSTS0 COMMENTS
Thapelo Manthata
6851 POSTS0 COMMENTS
Umr Jansen
6843 POSTS0 COMMENTS