javascript

subtractTwoNumbers()

Parameters: num1 (number), num2 (number)

num1, num2 are numbers to be subtracted

Returns: Subtraction result of num1 and num2 (number)

The subtractTwoNumbers function in JavaScript accepts two arguments and performs a subtraction operation. It returns the result of subtracting the second number from the first.

variables
functions
subtraction operation
return statement
Easy dificulty

Crafting a JavaScript Function to Subtract Two Numbers

Greetings Programmer! In this blog post, we will explore a simple, yet fundamental concept in Javascript - subtracting two numbers. Don't worry, we will break it down into easy steps. By the end, you'll be able to write a function to perform this operation. Stay tuned as we dive deeper into this programming adventure.

Step 1

The first step is to create a function that takes two parameters. These parameters represent the two numbers we will be subtracting.

function subtractNumbers(a, b) {

}

Step 2

After declaring the function, we now have to implement the subtraction logic. In JavaScript, the subtraction is performed with the minus symbol (-). Inside the body of subtractNumbers, we subtract the second number b from the first number a.

function subtractNumbers(a, b) {
    let difference = a - b;
}

Step 3

Now that the subtraction operation is being done, we need to provide the output of the function. This can be done by using the return statement, which stops the execution of a function and returns a value from that function.

function subtractNumbers(a, b) {
    let difference = a - b;
    return difference;
}

Step 4

At this point, our function is ready to use. We can call subtractNumbers with any two numbers as parameters, and it will return the result.

console.log(subtractNumbers(10, 7));  // Output: 3

Conclusion

Here's the complete function as it would appear in your code. This function will take in two numbers as inputs and return the difference.

function subtractNumbers(a, b) {
    let difference = a - b;
    return difference;
}
console.log(subtractNumbers(10, 7));  // Output: 3

Learn function in:

Subtraction

Subtraction is the operation of deducting numbers.

Learn more

Mathematical principle

The subtractTwoNumbers function employs the mathematical principle of subtraction, which is one of the basic arithmetic operations. In subtraction, we 'take away' one number from another to find the difference. For example, in `subtractTwoNumbers(5, 3)`, 3 is subtracted from 5 giving a result of 2.

Learn more