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.
Basic Subtraction Operation:
4.2.
JavaScript
4.3.
Using in Calculations:
4.4.
JavaScript
4.5.
Chaining with Other Lodash Operations:
4.6.
JavaScript
4.7.
Balancing Budgets:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.subtract() differ from native JavaScript subtraction?
5.2.
Can _.subtract() handle non-numeric inputs?
5.3.
Is _.subtract() necessary for simple subtraction operations?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.subtract() Method

Author Rinki Deka
0 upvote

Introduction

Subtraction is one of the basic arithmetic operations, and while JavaScript natively supports it, Lodash offers a utility method _.subtract() for this purpose. This method simplifies the process of subtracting one number from another, enhancing readability and consistency, especially when used in conjunction with other Lodash methods in data processing tasks.

Lodash _.subtract() Method

This article will delve into the Lodash _.subtract() Method, discussing its syntax, applications, and utility, illustrated through examples and Frequently Asked Questionss.

Why This Function is Used

The _.subtract() function is used to subtract one number from another. It provides a clear and explicit way to perform subtraction, which can be particularly helpful in complex expressions or when you want to maintain a consistent style using Lodash utilities. This method ensures that the subtraction operation is easily identifiable in the code, improving maintainability and readability.

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: 

_.subtract(minuend, subtrahend)

Parameters:

  • minuend (Number): The first number (from which you subtract).
     
  • subtrahend (Number): The second number (which is subtracted).

Return Value: 

(Number) - Returns the difference of minuend and subtrahend.

Examples 

Basic Subtraction Operation:

  • JavaScript

JavaScript

var _ = require('lodash');

console.log(_.subtract(6, 4));

Output: 

2


Demonstrates a simple subtraction operation using _.subtract().

Using in Calculations:

  • JavaScript

JavaScript

var total = 100;

var discount = 20;

var finalPrice = _.subtract(total, discount);

console.log(finalPrice);

Output: 

80


Shows how _.subtract() can be used in a basic arithmetic calculation to determine a final price after discount.

Chaining with Other Lodash Operations:

  • JavaScript

JavaScript

var result = _([10, 20]).map(x => _.subtract(x, 5)).value();

console.log(result);

Output: 

[5, 15]


An example of chaining _.subtract() with other Lodash methods for array manipulation.

Balancing Budgets:

  • JavaScript

JavaScript

var budget = 1000;

var expenses = [300, 200, 150];

var remainingBudget = _.reduce(expenses, (result, expense) => _.subtract(result, expense), budget);

console.log(remainingBudget);

Output: 

350


Demonstrates using _.subtract() in budget calculations by iterating over an array of expenses.

Frequently Asked Questions

How does _.subtract() differ from native JavaScript subtraction?

Functionally, _.subtract() does the same as native JavaScript subtraction. However, it provides more readability and integrates seamlessly with Lodash's functional programming style and chaining capabilities.

Can _.subtract() handle non-numeric inputs?

If non-numeric values are passed to _.subtract(), the result may be NaN (Not a Number), similar to JavaScript's native subtraction behavior with invalid inputs.

Is _.subtract() necessary for simple subtraction operations?

For basic subtraction, native JavaScript operations are usually sufficient. _.subtract() is more about maintaining coding style consistency, particularly in projects that heavily utilize Lodash for data manipulation.

Conclusion

Lodash's _.subtract() method offers a straightforward and readable way to perform subtraction operations in JavaScript. It fits well within the Lodash ecosystem, providing a consistent approach to arithmetic alongside other data manipulation tasks.

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 _.includes() Method
Next article
Lodash _.before() Method
Live masterclass