Navigate back to the homepage

react js convert string to number

Infinitbility
React
July 3rd, 2022 · 1 min read
react js convert string to number

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";
2
3// using plus operator
4let 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

  1. take an example of a string state
  2. use the plus operator
  3. print the converted number on the page screen
1import React, { useState, useEffect } from "react";
2export default function App() {
3 const [sampleData, setSampleData] = useState("25345");
4
5 useEffect(() => {
6 // using plus operator
7 let num = +sampleData;
8 setSampleData(num)
9 console.log(num)
10 }, []);
11
12 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.

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 get length of array in react js?

how to get length of array in react js?

An example of get length of array in react js with a step-by-step example.

July 2nd, 2022 · 1 min read
how to get length of object in react js?

how to get length of object in react js?

An example of find length of object in react js with a step-by-step example.

July 2nd, 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]