Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To convert a string to a number in react js, you can use the + plus operator it’s the fastest way to convert a string to a number.

In the following example, we will take the sample string variable, and convert it into an integer using the plus operator.

let text = "24325";

// using plus operator
let num = +text;
console.log(num) // 24325

Today, I’m going to show you How do i convert string to number in react js, as above mentioned here, I’m going to use the plus operator and string interpolation.

Let’s start today’s tutorial how do you convert string to number in react js?

In this example, we will do

  1. take an example of a string state
  2. use the plus operator
  3. print the converted number on the page screen
import React, { useState, useEffect } from "react";
export default function App() {
  const [sampleData, setSampleData] = useState("25345");

  useEffect(() => {
    // using plus operator
    let num = +sampleData;
    setSampleData(num)
    console.log(num)
  }, []);

  return (
    <div className="App">
      <h1>{`sampleData type ${typeof sampleData}`}</h1>
    </div>
  );
}

In the above react js example, we have taken the example of converting a string to a number and printed output in the console and page screen.

I hope it helps you, All the best đź‘Ť.