On Apr 25, 3:37 pm, Ananth Raghuraman <
araghuram...@gmail.com> wrote:
> Thanks for pointing me to the native functions!
> In the meantime I also found 1.8 has the Number object/function.
The Number constructor is a built-in ECMAScript function. When called
as a function, it does type conversion:
var num = '1';
// Convert to number
num = Number(num);
How it works is covered by Section 15.7.1.1 of the ECMAScript language
specification.
> I just did Number(mynumberstring) to convert mynumberstring to a number.
It is faster to use unary +:
var num = '1';
// Convert to number
num = +num;
Also note that any arithmetic operation on a string other than + will
always perform type conversion:
var a = '5';
alert( a * a); // 25
> I guess Number also accepts an object
Yes, it's explained in section 9.3 on type conversion. The result of
calling Number(object) is, more or less, Number(object.toString()),
however read the ECMAScript specification for a full explanation.
> or another Number as argument..
Yes, but calling Number(number) simply returns the number (Sec. 9.3).
--
Rob