What is Underscore.js or Underscore in JavaScript?

Do you want to know the power of underscore.js or underscore in JavaScript? Read on!

In this article, we will learn about underscore.js, a JavaScript library that provides a collection of utility functions for common programming tasks.

You’ll explore the usage of the underscore (_) symbol in JavaScript and its various purposes, including creating space in variable names, disregarding function parameters, and manipulating arrays and objects.

Let’s get started with underscore.js today and write more efficient and readable code.

What is underscore.js?

Underscore.js is a JavaScript library that provides a collection of utility functions for common programming tasks.

It is designed to be lightweight and easy to use, and it doesn’t extend any core JavaScript objects.

It simply means that you can use it without worrying about conflicts with other libraries or frameworks.

Some of the most popular features of Underscore.js include its support for functional programming concepts like map, reduce, and filter, as well as its ability to work with objects and collections in a variety of ways.

How underscore.js works?

Underscore.js is like a toolbox for JavaScript programmers. It has handy tools to help with everyday coding tasks, like working with lists, objects, and more.

It won’t mess up your existing JavaScript code. You can use it in your web browser: or with Node.js.

To get it in your browser, just download the latest underscore-min.js file from their website and add it to your code.

If you’re using Node.js, you can install it with npm or yarn.

npm install underscore

or

yarn install underscore

Once it’s set up, you can use Underscore.js tools in your code. For example, if you want to find the biggest number in a list, you can use the _.max() function.

Here’s an example:

<!DOCTYPE html>
<html>
 
<head>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
    </script>
</head>
 
<body>
    <script type="text/javascript">
        let samplegrades = [92, 90, 99, 98, 95];
        console.log(_.max(samplegrades)); ✅
    </script>
</body>
 
</html>

Output:

99

People like Underscore.js because it can do many common jobs with data and plays well with other JavaScript tools like jQuery for web stuff.

In short, Underscore.js makes JavaScript coding easier and cleaner by giving you helpful tools.

What is the _ symbol in JavaScript?

The underscore symbol _ is a valid identifier in JavaScript, and it is often used as a function parameter.

A single underscore is a convention used by some JavaScript programmers to indicate to other programmers that they should “ignore this binding/parameter.”

Since JavaScript doesn’t do parameter-count checking, the parameter could have been omitted entirely.

This symbol is often used (by convention again) in conjunction with fat-arrow functions to make them even terser and readable, like this:

const fun = _ => console.log('Hi, Welcome to Itsourcecode!')
fun()

In this case, the function needs no params to run, so the developer has used the underscore as a convention to indicate this. The same thing could be written like this:

const fun = () => console.log('Hi, Welcome to Itsourcecode!')
fun()

The difference is that the second version is a function with no parameters, but the first version has a parameter called _ that is ignored. These are different though and the second version is safer, if slightly more verbose (1 extra character).

Also, consider a case like:

arr.forEach(function (_, i) {..}) 

Where _ indicates the first parameter is not to be used.

The use of underscores like this can get very confusing when using the popular lodash or underscore libraries.

In simple understanding, the underscore symbol (_) is like any other letter or number you can use in a variable or function name.

It doesn’t add any new abilities. However many programmers use it as a kind of secret code to show that something should be kept private.

What is the used of underscore (_) in JavaScript?

The underscore symbol (_) serves several purposes in JavaScript.

It can be used to create space in variable names and to disregard function parameters, and it’s also employed in libraries like underscore.js to manipulate arrays and objects.

Usage of underscore (_) in JavaScript

  1. Using an underscore in JavaScript is frequently done to add clarity to variable names, making it simpler to comprehend their purpose.

For instance, you might write:

let sample_variable = 100;

As you have noticed, it gives space to our variable name to clearly convey the meaning of the variable.

It’s common to use underscores to separate words in variable names, following a convention known as “snake_case.”

For example:

let user_name = "It_sourcecode";

  1. Even though JavaScript doesn’t provide built-in support for private variables, some developers follow a convention using underscores to signal that a variable should be considered private.

    This means it shouldn’t be accessed directly from outside the object or function scope.

For example:

function employee() {
    let _age = 18; ✅ // Private variable
    
    this.getAge = function () {
        return _age;
    };
}

const Itsourcecode = new employee(); 
console.log(Itsourcecode.getAge()); //Accessing the private variable

Output:

18

  1. The underscore character (_) is commonly utilized as a way to specify a specific area or name in different JavaScript libraries and frameworks.

    For instance, the well-known JavaScript library called Underscore.js is frequently brought into a program using the underscore character (_).
const _ = require('underscore'); ✅
 

Conclusion

To sum up, underscore.js is a powerful JavaScript library that provides a collection of utility functions for common programming tasks.

It is designed to be lightweight and easy to use, and it doesn’t extend any core JavaScript objects, which means that you can use it without worrying about conflicts with other libraries or frameworks.

Additionally, the underscore symbol _ is a valid identifier in JavaScript and is often used as a function parameter to indicate that it should be ignored.

We hope this article has provided you with enough information to help you understand the JavaScript underscore.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Leave a Comment