In many instances, Java Script automatically converts numbers to strings, but you can use the String object constructor or the toString method to explicitly convert a number to a string.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

Win $3,500.00 worth of gas!

Convert a Number to a String

In many instances, Java Script automatically converts numbers to strings. For example, anytime you display a number using document.write or alert, Java Script automatically converts the number to a string for display.

var n = 10 + 2;
alert(n);

var n = 10 + 2;
document.write(n);

Both of the code snippets above will cause the string "12" to display.

A quick way to convert a number to a string is to use the string concatenation operator (+) as shown below.

var t = 118;
var s = t + " degrees f";
document.write(s);

The code above will display the string "118 degrees f".

You don't actually need any characters in the concatenated string to convert a number to a string, as shown below.

var n = 3.14152;
alert(typeof n);
n = 3.14152 + "";
alert(typeof n);

The fist message box will display the type of n as "number", after concatenating an empty string to the variable n, the second message box will display the type of n as "string".

The code below casts the number 3.14152 to a string by passing it to a String object constructor.

var n = 3.14152;
var s = String(n);
alert(typeof s);

The proper way to convert a number to a string is to use the Number object's toString method, as shown below.

var n = 3.14152;
s = n.toString();
alert(typeof s);

why would you want to convert a number to a string? After performing a calculation, you may need to format the result for display. The code shown below formats the number 32.6738 to $ 32.67 for display as a currency value.

var k = 32.6738;
m = k.toString();
var n = m.substring(0,m.indexOf(".") + 3);
var amt = "$ " + n;
alert(amt);

In many instances, Java Script automatically converts numbers to strings, but you can use the String object constructor or the toString method to explicitly convert a number to a string.

Web Design Sections

RSS Feed RSS Feed

Java Script Quick Reference
Java Script Data Types
Java Script Reserved Words
Java Script Arithmetic Operators
Comparison Operators
Java Script Arrays
Java Script Character Encoding and Decoding
The if/else Structure
The switch/case Structure
The for Loop
The while Loop
The break Statement
The continue Statement
JavaScript Math Object
Round a Number
Determine Absolute Value
Determine Minimum and Maximum
Generating Random Numbers
Java Script Trigonometric Methods
Java Script Number Object
Format a Number as Currency
Java Script Strings
Compare Two Strings
Find a Character or a Substring Within a String
Include a Quote Character in a String
Include a Backslash Character in a String
Define Lines in a String
Use Escape to Replace Dangerous Characters
Convert a Number to a String
Convert a String to a Number
The Document Object Model (DOM)
Accessing Web Page Elements
Interactively Set Webpage Colors
Get Webpage File Date and File Size
Dueling Windows
Cookie Power Made Easy

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269