Automatic type conversions

A

Aaron

I know now (thanks to this board) that there are numerous ways to force
a type conversion from a string to a number (such as +, Number(), adding
1, parseInt(), etc).
I'm curious, though...is there any way to force a type conversion the
other way, that is, from a number to a string? Previously, I've just
been assigning numbers to the value property of whatever element I want
to change, which seems to automatically force the appropriate type
conversion (number to string). This seems sloppy though - is there a
way to overtly force this type conversion without having to resort to this?

Thanks,
Aaron
 
M

Michael Winter

I know now (thanks to this board) that there are numerous ways to force
a type conversion from a string to a number (such as +, Number(), adding
1, parseInt(), etc).
I'm curious, though...is there any way to force a type conversion the
other way, that is, from a number to a string? Previously, I've just
been assigning numbers to the value property of whatever element I want
to change, which seems to automatically force the appropriate type
conversion (number to string). This seems sloppy though - is there a
way to overtly force this type conversion without having to resort to
this?

Concatenation with a string:

var str = 6 + '';

or using the String() constructor as a function:

var str = String( 6 );

will convert the number to a plain string. If you want to format the value
as a floating-point number:

<URL:http://jibbering.com/faq/#FAQ4_6>

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top