Navigate back to the homepage

How to subtract two numbers in react js?

October 12th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To subtract two numbers in react js, use the - subtraction operator it will subtract it when your value datatype is a number else if your value datatype is string first convert it using parseInt() and then perform the subtraction operation.

In the following example, we will take the sample numbers, and strings and perform subtract operations using the - subtraction operator.

Subtracting Two numbers example

1let num1 = 30;
2let num2 = 34;
3num2 - num1; // 4

Subtracting Two string numbers example

1let num1 = "30";
2let num2 = "34";
3parseInt(num2) - parseInt(num1); // 4

Today, I’m going to show you How do i subtract two numbers in react js, as mentioned above here, I’m going to use the - subtraction operator.

Let’s start today’s tutorial on how do you subtract two numbers in react js.

In this example, we will do

  • take an example of a number and string number state
  • subtracting two numbers example
  • subtracting two string numbers example
  • print the output on the page screen
1import React, { useState } from "react";
2export default function App() {
3 const [firstNum, setFirstNum] = useState(24);
4 const [secondNum, setSecondNum] = useState(20);
5 const [firstStr, setFirstStr] = useState("24");
6 const [secondStr, setSecondStr] = useState("20");
7 return (
8 <div className="App">
9 <h1>{`subtracting two numbers example`}</h1>
10 <p>{firstNum - secondNum}</p>
11 <h1>{`subtracting two string numbers example`}</h1>
12 <p>{parseInt(firstStr) - parseInt(secondStr)}</p>
13 </div>
14 );

In the above react js example, we have taken the sample numbers and string numbers state, and performed subtracting two numbers example, and subtracting two string numbers example.

I hope it helps you, All the best 👍.


Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide



More articles from Infinitbility

How to find the length of a string in an array in Javascript??

An example of get length of array string in javascript with the step-by-step guide.

October 11th, 2022 · 1 min read

How do you get the first letter of string Javascript?

An example of get the first letter of a string in javascript with the step-by-step guide.

October 10th, 2022 · 1 min read
© 2020–2022 Infinitbility
Link to $ to $ to $ to $ to $ to $mailto:[email protected]