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.
1let text = "24325";23// using plus operator4let num = +text;5console.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
- take an example of a string state
- use the plus operator
- print the converted number on the page screen
1import React, { useState, useEffect } from "react";2export default function App() {3 const [sampleData, setSampleData] = useState("25345");45 useEffect(() => {6 // using plus operator7 let num = +sampleData;8 setSampleData(num)9 console.log(num)10 }, []);1112 return (13 <div className="App">14 <h1>{`sampleData type ${typeof sampleData}`}</h1>15 </div>16 );17}
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 👍.
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.