integer length in javascript

Stoopid question time!

I know that in JavaScript you have to convert the integer to a string:

var num = 1024;
len = num.toString().length;
console.log(len);

My question is this: Why is there no get length property for integers in JavaScript? Is it something that isn't used that often?

Answers


Well, I don't think providing length properties to number will be helpful. The point is the length of strings does not change by changing its representation.

for example you can have a string similar to this:

var b = "sometext";

and its length property will not change unless you actually change the string itself.

But this is not the case with numbers.

The same number can have these two representations:

 var a = 23e-1;
and 
 var b = 2.3;

So with these two representation its clear that same number can have multiple representation and so, if you have length property with numbers it will have to change with the representation of the numb.er


you must set variable toString() first, like this:

var num = 1024,
str = num.toString(),
len = str.length;

console.log(len);

Need Your Help

jQuery.get function not working in Github Pages

jquery github

I am trying to build a project-page setup that auto populates the project details in Github using Github API. I have put up my HTML and JavaScript files in the gh-pages branch. But the jQuery.get()...

Using sed to replace entire phrase

regex bash shell sed sh

I'm using ShellScript to edit my bind dns configuration file, when add and remove zone references.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.