Subtracting Numbers in JavaScript

Subtracting Numbers in JavaScript

Subtracting numbers in JavaScript is just as important as adding them. Whenever you calculate a difference, reduce a value, track remaining balance, or compare quantities, subtraction is involved. Even simple things like checking how much money is left or how many items remain after a sale rely on subtraction working correctly.

JavaScript makes subtraction very easy to understand, especially for beginners. With a simple minus sign, you can subtract whole numbers, decimal values, or even user-provided input. Learning how subtraction works also helps you understand how JavaScript handles numbers, variables, and data types, which are essential building blocks for any program.

Program 1: Subtracting Two Whole Numbers

This program shows how to subtract one integer from another using basic variables. The values are predefined so you can focus on the subtraction itself.

let totalApples = 20;
let eatenApples = 8;

let remainingApples = totalApples - eatenApples;
console.log(remainingApples);

In this example, two variables store whole numbers. The minus sign subtracts the second value from the first and stores the result in a new variable. This is useful for simple calculations such as counting remaining items or tracking decreases.

Program 2: Subtracting Decimal Numbers

This program demonstrates how JavaScript handles subtraction with floating-point numbers.

let accountBalance = 150.75;
let withdrawalAmount = 45.50;

let newBalance = accountBalance - withdrawalAmount;
console.log(newBalance);

JavaScript understands decimal values naturally, so subtraction works without extra steps. This approach is very common in financial applications where precise values like prices, taxes, or balances are involved.

Program 3: Subtracting Mixed Numbers

This program shows subtraction between a whole number and a decimal number.

let totalDistance = 50;
let distanceCovered = 18.5;

let remainingDistance = totalDistance - distanceCovered;
console.log(remainingDistance);

JavaScript automatically converts the whole number into a decimal before subtracting. This makes mixed-number subtraction simple and beginner-friendly, especially in real-world scenarios like distance tracking or time calculations.

Program 4: Subtracting Numbers Stored as Strings

This program explains how to correctly subtract values that are stored as text.

let firstValue = "100";
let secondValue = "30";

let difference = Number(firstValue) - Number(secondValue);
console.log(difference);

Values inside quotes are treated as text, not numbers. Converting them using Number() ensures correct subtraction. This is especially useful when dealing with form inputs or data collected from users.

Program 5: Subtracting Numbers Using User Input

This program allows the user to enter two numbers and subtracts the second from the first.

let firstInput = prompt("Enter the first number:");
let secondInput = prompt("Enter the second number:");

let result = Number(firstInput) - Number(secondInput);
alert(result);

The prompt function collects user input as text. Converting the input into numbers ensures proper subtraction. This is commonly used in interactive web pages where users provide values dynamically.

Program 6: Subtracting Numbers with a Function

This program shows how subtraction can be placed inside a reusable function.

function subtractNumbers(startValue, endValue) {
  return startValue - endValue;
}

let finalResult = subtractNumbers(40, 15);
console.log(finalResult);

Functions allow you to reuse logic without rewriting code. By placing subtraction inside a function, beginners can apply the same logic to different values easily as programs grow larger.

Frequently Asked Questions (FAQ)

This section answers common beginner questions about subtracting numbers in JavaScript and helps clear up confusion.

Q1. Why does subtraction work even when values are strings?
JavaScript automatically tries to convert strings into numbers during subtraction, but it is still safer to convert them manually.

Q2. Can JavaScript subtract negative numbers?
Yes, JavaScript handles negative numbers without any problem.

Q3. Is subtraction different for decimals?
No, subtraction works the same way for decimals and whole numbers.

Q4. What happens if the second number is larger than the first?
The result will be a negative number, which JavaScript handles normally.

Q5. Should I always use functions for subtraction?
For small scripts it is not required, but functions help keep code clean and reusable.

Conclusion

Subtracting numbers in JavaScript is simple, clear, and very powerful once you understand it. In this article, you learned how to subtract whole numbers, decimal values, mixed numbers, strings, and even user input. You also saw how functions make subtraction reusable and easier to manage.

The best way to get comfortable is to practice. Try changing the values, using different inputs, and writing small examples of your own. With steady practice, subtraction will feel natural, and you will be ready to handle more advanced JavaScript logic with confidence.

Scroll to Top