Hi Friends 👋,
Welcome To Infinitbility! ❤️
To get the length of the number in react js, convert the number to string using the toString()
method and use the String length property it will return of your number char count.
The length property of a String object contains the length of the string, in UTF-16 code units. length is a read-only data property of string instances.
In the following example, we will take the sample number, convert it into string and use string length to get the length of your number.
1let num = 22897878434;23console.log(num.toString().length) // 11
Today, I’m going to show you How do i find the length of the integer in react js, as above mentioned here, I’m going to use the toString()
method, and the String length property.
Let’s start today’s tutorial how do you get the length of the number in react js?
In this example, we will do
- take an example of a number state
- use the
toString()
method, and string length - print the length of the number on the page screen
1import React, { useState, useEffect } from "react";2import "./styles.css";3export default function App() {4 const [sampleNumber, setSampleNumber] = useState(35465734535);56 return (7 <div className="App">8 <h1>{`sampleNumber length is ${sampleNumber.toString().length}`}</h1>9 </div>10 );11}
In the above react js example, we have taken the sample number state, used string toString()
and string length, and printed it on the 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.