Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Performing the subtraction operation without the subtraction operator in JavaScript
We are required to write a JavaScript function that takes in two numbers and returns their difference but without using the (-) sign.
Therefore, let’s write the code for this function −
Example
The code for this will be −
const num1 = 56;
const num = 78;
const subtractWithoutMinus = (num1, num2) => {
if(num2 === 0){
return num1;
};
return subtractWithoutMinus(num1 ^ num2, (~num1 & num2) << 1);
};
console.log(subtractWithoutMinus(num, num1));
Output
The output in the console will be −
22
Advertisements
