Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
As a Default Function in Higher-Order Operations:
4.2.
JavaScript
4.3.
Using in Functional Compositions:
4.4.
JavaScript
4.5.
As a Default Callback:
4.6.
Filtering with Identity Function:
4.7.
JavaScript
5.
Frequently Asked Questions
5.1.
Is _.identity() just a redundant wrapper around a variable?
5.2.
Can _.identity() be used with multiple arguments?
5.3.
How does _.identity() benefit readability and maintainability?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.identity() Method

Author Riya Singh
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

In functional programming, and particularly in JavaScript, there are scenarios where a function that returns its first argument as-is can be surprisingly useful. This is where Lodash's _.identity() method comes into play. It is a simple utility function that returns the first argument it receives, unmodified. 

Lodash _.identity() Method

This method is particularly useful in functional compositions, callbacks, and scenarios where a default or fallback function is required.

Why This Function is Used

The _.identity() function is used as a utility function in various scenarios where an operation needs to pass its input directly to the output without any transformation. It's often used in higher-order functions, as a default callback, or as a placeholder in functional compositions to represent an operation that does not change the data.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax, Parameter and Return Value

Syntax: 

_.identity(value)

Parameters:

value: The value to return.

Return Value: 

Returns the given value.

Examples 

As a Default Function in Higher-Order Operations:

  • JavaScript

JavaScript

var _ = require('lodash');

var array = [1, 2, 3];

var result = _.map(array, _.identity);

console.log(result);

Output: 

[1, 2, 3]


Demonstrates using _.identity() as a default iteratee in a map operation.

Using in Functional Compositions:

  • JavaScript

JavaScript

var double = n => n * 2;

var triple = n => n * 3;

var compose = _.flow([double, _.identity, triple]);

console.log(compose(2));

Output: 

12 (2 * 2 * 3)


Shows using _.identity() as a no-operation function in a functional composition.

As a Default Callback:

function fetchData(callback) {
  callback = callback || _.identity;
  // Fetch data and return it through the callback
  callback('data');
}
fetchData(); 


// Doesn't throw an error, silently ignores the lack of callback

An example of using _.identity() as a default callback in a function.

Filtering with Identity Function:

  • JavaScript

JavaScript

var mixedArray = [0, 1, false, 2, '', 3];

var compactArray = _.filter(mixedArray, _.identity);

console.log(compactArray);

Output:

[1, 2, 3]


Demonstrates using _.identity() to filter truthy values from an array.

Frequently Asked Questions

Is _.identity() just a redundant wrapper around a variable?

While it might seem redundant, _.identity() is useful in functional programming paradigms, especially in places where a function is expected rather than a direct value.

Can _.identity() be used with multiple arguments?

_.identity() only returns its first argument. Any additional arguments passed to it will be ignored.

How does _.identity() benefit readability and maintainability?

Using _.identity() can make it clear to future readers of the code that a function intentionally does not transform its input, improving code readability and intent.

Conclusion

Lodash's _.identity() method is a simple yet powerful tool in functional programming, providing a straightforward way to pass values through a series of operations unaltered. It's especially useful in creating clear and concise code in scenarios that involve functional compositions, callbacks, and default operations.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Previous article
Lodash _.camelCase() Method
Next article
Lodash _.findLastKey() Method
Live masterclass