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

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

Dividing Two string numbers example

let num1 = "3";
let num2 = "9";
parseInt(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
import React, { useState } from "react";
export default function App() {
  const [firstNum, setFirstNum] = useState(3);
  const [secondNum, setSecondNum] = useState(9);
  const [firstStr, setFirstStr] = useState("3");
  const [secondStr, setSecondStr] = useState("9");
  return (
    <div className="App">
      <h1>{`dividing two numbers example`}</h1>
      <p>{ secondNum / firstNum }</p>
      <h1>{`dividing two string numbers example`}</h1>
      <p>{parseInt(secondNum) / parseInt(firstNum)}</p>
    </div>
  );
}

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