Using toString() in JavaScript to convert data types to String

By: Nicholas C. Zakas Emailed: 1606 times Printed: 2076 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The interesting thing about JavaScript primitive values for Booleans, numbers, and strings is that they are pseudo-objects, meaning that they actually have properties and methods. For example, to get the length of a string, you can do the following:

var sColor = “blue”;

alert(sColor.length); //outputs “4”

Even though the value “blue” is a primitive string, it still has a length property holding the size of the string. To that end, the three main primitive values, Booleans, numbers, and strings, all have a toString() method to convert their value to a string.

You may be asking, “Isn’t it ridiculously redundant to have a toString() method for a string?” Yes, it is. But ECMAScript defines all objects, whether they are pseudo-objects representing primitive values or full-fledged objects, to have a toString() method. Because the string type falls in the category of pseudo-object, it also must have a toString() method.

The Boolean toString() method simply outputs the string “true” or “false”, depending on the value of the variable:

var bFound = false;

alert(bFound.toString()); //outputs “false”

The Number toString() method is unique in that it has two modes: default and radix mode. In default mode, the toString() method simply outputs the numeric value in an appropriate string (whether that is integer, floating point, or e-notation), like this:

var iNum1 = 10;

var fNum2 = 10.0;

alert(iNum1.toString()); //outputs “10”

alert(fNum2.toString()); //outputs “10”

In default mode, the Number’s toString() method always returns the decimal representation of the number, regardless of how you originally specified it. Therefore, numbers specified by octal or hexadecimal literals are output as decimal.

When you use the Number’s toString() method in radix mode, it is possible to output the number using a different base, such as 2 for binary, 8 for octal, or 16 for hexadecimal. The radix is just a fancy name for the base to convert to, and it is specified as an argument to the toString() method:

var iNum = 10;

alert(iNum1.toString(2)); //outputs “1010”

alert(iNum1.toString(8)); //outputs “12”

alert(iNum1.toString(16)); //outputs “A”

In the previous example, the number 10 is output in three different ways: binary, octal, and hexadecimal. This functionality can be very useful for dealing with numbers in HTML, which use hexadecimal representations for each color.

Calling toString(10) on a number is the same as calling toString(); they both return the decimal equivalent of the number.


Javascript Home | All Javascript Tutorials | Latest Javascript Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

Can u please help me to implement traversing forwarding as well as backward traversing for android appliactions. If you having idea pls share urgently...........

View Tutorial          By: Rahi at 2015-09-18 11:12:32

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Nicholas C. Zakas
call() and apply() methods in Javascript
this keyword sample in Javascript
Math object and Math functions in Javascript
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Using toString() in JavaScript to convert data types to String
Javascript Basics
History and evolution of Javascript

More Tutorials in Javascript
Use WinSCP to transfer log files remotely using Javascript
Verifying user input in JavaScript
Pause execution in JavaScript
Javascript to display client date and time on webpage
Getting Browser's height and width using Javascript
Highlighting text on a page using CSS
Using revealTrans to do page transitions in Javascript
Password protect a web page using Javascript
Diabling Right Click option in a browser using Javascript
Scrolling message on the status bar using Javascript
window.frames[i] in Javascript
Form validation using Javascript
Using && in Javascript
The if statement in Javascript
do-while, while, for and for-in loops in Javascript

More Latest News
Most Viewed Articles (in Javascript )
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Show how many characters remaining in a html text box using javascript
History and evolution of Javascript
List of Keywords and reverved words in Javascript
Using toString() in JavaScript to convert data types to String
Using ! and !! in Javascript
window.frames[i] in Javascript
Getting Browser's height and width using Javascript
Javascript Basics
do-while, while, for and for-in loops in Javascript
break and continue in Javascript
Form validation using Javascript
Highlighting text on a page using CSS
Using revealTrans to do page transitions in Javascript
Password protect a web page using Javascript
Most Emailed Articles (in Javascript)
Getting Browser's height and width using Javascript
Click to remove an element from a select html form using javascript
List of Keywords and reverved words in Javascript
Javascript to display client date and time on webpage
Show how many characters remaining in a html text box using javascript
History and evolution of Javascript
Javascript Basics
Using ! and !! in Javascript
do-while, while, for and for-in loops in Javascript
switch in Javascript
this keyword sample in Javascript
window.frames[i] in Javascript
Use WinSCP to transfer log files remotely using Javascript
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Typecasting in Javascript