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
var _ = require('lodash');
console.log(_.subtract(6, 4));

You can also try this code with Online Javascript Compiler
Run Code
Output:
2
Demonstrates a simple subtraction operation using _.subtract().
Using in Calculations:
JavaScript
var total = 100;
var discount = 20;
var finalPrice = _.subtract(total, discount);
console.log(finalPrice);

You can also try this code with Online Javascript Compiler
Run Code
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
var result = _([10, 20]).map(x => _.subtract(x, 5)).value();
console.log(result);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[5, 15]
An example of chaining _.subtract() with other Lodash methods for array manipulation.
Balancing Budgets:
JavaScript
var budget = 1000;
var expenses = [300, 200, 150];
var remainingBudget = _.reduce(expenses, (result, expense) => _.subtract(result, expense), budget);
console.log(remainingBudget);

You can also try this code with Online Javascript Compiler
Run Code
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 DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.