Navigate back to the homepage

How to divide two numbers in react js?

Infinitbility
React
October 14th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

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

In the following example, we will take the sample numbers, and strings and perform the divide operation using the / division operator.

Dividing Two numbers example

1let num1 = 3;
2let num2 = 9;
3num2 / num1; // 3

Dividing Two string numbers example

1let num1 = "3";
2let num2 = "9";
3parseInt(num2) / parseInt(num1); // 3

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

Let’s start today’s tutorial how do you divide two numbers in react js?

In this example, we will do

  • take an example of a number and string number state
  • dividing two numbers example
  • dividing 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(3);
4 const [secondNum, setSecondNum] = useState(9);
5 const [firstStr, setFirstStr] = useState("3");
6 const [secondStr, setSecondStr] = useState("9");
7 return (
8 <div className="App">
9 <h1>{`dividing two numbers example`}</h1>
10 <p>{ secondNum / firstNum }</p>
11 <h1>{`dividing two string numbers example`}</h1>
12 <p>{parseInt(secondNum) / parseInt(firstNum)}</p>
13 </div>
14 );
15}

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

I hope it helps you, All the best 👍.

Output

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

Tutorials

Categories

More articles from Infinitbility

How to multiply two numbers in react js?

An example of multiplying two numbers in react js with the step-by-step guide.

October 13th, 2022 · 1 min read

How to subtract two numbers in react js?

An example of subtracting two numbers in react js with the step-by-step guide.

October 12th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]