How can I find the length of a number in JavaScript?

Given a number, we have to find its length using JavaScript's "length" property.
Submitted by Pratishtha Saxena, on August 14, 2022

To get the length of a number, we use .length property.

JavaScript length Property

To find the length of any given number, there is a simple property of JavaScript objects for this. The length of a number can be determined by using the length property. This property can be used for strings and arrays in JavaScript. Therefore, this property is not applicable for an integer or float number.

Syntax:

str.length;

Where, str can either be string or array. It cannot be an integer or float number.

Hence, to find the length of a given number, first, we need to convert the given number to a string and then apply the .length property. For doing this, we will use the toString() method of JavaScript. As it says, it will convert the specified variable to a string data type.

Syntax:

num.toString();

Here, num can be any integer or float number that has to be converted into a string.

Let's understand how can we find the length of an integer or float number?

JavaScript examples to find the length of a number

Example 1:

const num = 5864;
var new_num = num.toString();
console.log(new_num.length);

Output:

4

Example 2:

const num = 58643.5496;
var new_num = num.toString();
console.log(new_num.length);

Output:

10

JavaScript Examples »



Related Examples



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.