LandonSchropp
LandonSchropp

Reputation: 10244

JavaScript Numbers

I read on the MDN Reintroduction to JavaScript that JavaScript numbers are a floating point precision type only, and that there are no integers in JavaScript. Yet JavaScript has two functions, parseInt and parseFloat`. After doing a bit of Googling, I'm getting mixed results.

So, does JavaScript abstract away the details of integers and doubles in its number type, or does JavaScript only have double precision Number types and the functions parseInt and parseFloat are poorly named?

Upvotes: 2

Views: 1366

Answers (6)

RobG
RobG

Reputation: 147403

The MDN reference is pretty good, there's nothing in these answers that isn't in that article. I suggest you read ECMA-262 4.3.19 to 21 to understand the difference between a number value, a number Type and a number Object (and probably the whole section to understand Types in general).

An important aspect of javascript is that it is not strongly typed. There is no floating point precision type, nor is there an integer type. There are values, Types and Objects.

Variables don't have a type, their values do. You can assign a value to a variable, and that value can have a Type of String, Number, Boolean, etc. Don't get confused between Types and Objects - you can't specify that a variable has a particular Type, you can only assign it a value that has a Type. e.g.

var num = '5'; // the value of num is a String Type
num = +num;    // its value is now a Number Type
num = num/2;   // its value is still a Number Type
               // but if printed looks like a float (2.5)

There is a Number object too (though rarely used) and javascript handily converts number primitives to objects for the sake of evaluating expressions:

(5).toString();  // 5 is converted to a Number object
                 // its toString method is called, returning '5'

In the above, the grouping operator is required as otherwise the 'dot' will be interpreted as a decimal place and the following 't' as a syntax error. It is essentially the reverse of parseInt.

The parseInt and parseFloat methods are used to convert a string Type value to a number Type value as shown in the MDN article. There are other methods, such as:

var num = '5';    // string Type
var x = num * 2;  // number Type

Variable x has a number Type value because multiplication will convert the string value of num to a number before doing multiplication.

var y = x + 'px';  // string Type

The '+' operator is overloaded - because one of the terms in the expression is a string, it will be treated as a concatenation operator so x will be converted to a string Type, 'px' will be concatenated and the string '10px' will be assigned to y, so its value is a string Type.

Read relevant parts of ECMA-262, it explains how it all works in detail.

Upvotes: 2

Josh Lee
Josh Lee

Reputation: 177584

JavaScript only has doubles, and that’s what both of the functions will return, but parseInt will effectively round toward zero.

> parseInt('9007199254740993.5')
9007199254740992
> parseFloat('9007199254740993.5')
9007199254740994

Upvotes: 0

Guffa
Guffa

Reputation: 700342

Although Javascript doesn't have any integer type, it does have some integer functions and operators.

The parseInt function does parse a string looking for a number in integer format, and does return an integer value although it's converted to the Number type.

Some examples of the integer operators are the << and >> shift operators, that work on 32 bit integers. The numbers that they operate on are converted to integers for the operation, and the result is an integer value converted to the Number type.

Upvotes: 0

bobince
bobince

Reputation: 536379

does JavaScript abstract away the details of integers and doubles in its number type, or does JavaScript only have double precision Number types

Yes.

A JS engine may choose to optimise some uses of Number to use integer math behind the scenes, but as far as JS apps are concerned, there is only Number, and it always behaves exactly like an IEEE double-precision float.

and the functions parseInt and parseFloat are poorly named?

Arguably, but parseFloatThatHappensToBeAWholeNumber would be a bit unwieldy. :-)

Upvotes: 8

Dave Newton
Dave Newton

Reputation: 160191

They're reasonably named; one parses strings-representing-ints, one parses strings-representing-floats. The underlying Number implementation doesn't affect what they're trying/able to parse.

But yes, JavaScript numbers are floats.

Upvotes: 1

Marc B
Marc B

Reputation: 360672

Those two functions are for extracting ints and floats from strings, but after the extract is complete, the numbers will STILL be stored internally as floats.

Upvotes: 0

Related Questions