React JS: Convert String to Number Javascript

Converting and using a string as a number in react apps. Javascript string to number conversion.

How to convert string to a number?

Using the Number Object for conversion

We can make use of the Number object to convert a string to a number.

React JS : Convert String to Number Javascript

    const amount = Number(inputValue);

    const convenienceFee = Number('20');

TypeScript React Example

Example Code with typescript type checking for string and number in react app.


    const inputValue : string = "250"

    const amount : number = Number(inputValue);

    const convenienceFee : number = Number('20');

Example Code String to Number conversion React JavaScript:


const handleInput (event) => {

  const amount = Number(event.target.value);

  console.log("Amount:" + amount );


}

About the Author: smartcoder

You might like

Leave a Reply

Your email address will not be published. Required fields are marked *