Typecasting in Javascript

By: Emiley J. Emailed: 1642 times Printed: 2110 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

It’s also possible to convert values using a process called type casting. Type casting allows you to access a specific value as if it were of a different type. Three type casts are available in JavaScript:

  • Boolean(value) – casts the given value as a Boolean
  • Number(value) – casts the given value as a number (either integer or floating-point)
  • String(value) – casts the given value a string

Casting a value using one of these three functions creates a new value that is a direct conversion of the original. This can lead to some unexpected results. The Boolean() type cast returns true when the value is a string with at least one character, a number other than 0, or an object (discussed in the next section); it returns false when the value is an empty string, the number 0, undefined , or null . The following code snippet can be used to test type casting as a Boolean:

var b1 = Boolean(“”); //false – empty string

var b2 = Boolean(“hi”); //true – non-empty string

var b3 = Boolean(100); //true – non-zero number

var b4 = Boolean(null); //false - null

var b5 = Boolean(0); //false - zero

var b6 = Boolean(new Object()); //true – object

The Number() type cast works in a manner similar to parseInt() and parseFloat() , except that it converts the entire value, not just part of it. Remember that parseInt() and parseFloat() only convert up to the first invalid character (in strings), so “4.5.6” becomes “4.5” . Using the Number() type cast, “4.5.6” becomes NaN because the entire string value cannot be converted into a number. If a string value can be converted entirely, Number() decides whether to use parseInt() or parseFloat() . The following table illustrates what happens when Number() is used on various values:

Usage Result

Number(false) 0

Number(true) 1

Number(undefined) NaN

Number(null) 0

Number(“5.5”) 5.5

Number(“56”) 56

Number(“5.6.7”) NaN

Number(new Object()) NaN

Number(100) 100

The last type cast, String() , is the simplest because it can accurately convert any value to a string value. To execute the type cast, it simply calls the toString() method of the value that was passed in, which converts 1 to “1”, true to “true”, false to “false”, and so on. The only difference between type casting as a string and using toString() is that the type cast can produce a string for a null or undefined value without error:

var s1 = String(null); //”null”

var oNull = null;

var s2 = oNull.toString(); //won’t work, causes an error

Type casting is very helpful when dealing with the loosely typed nature of JavaScript, although you should ensure that only proper values are used.


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(4)


1. View Comment

Typecasting proved very much helpful to me...esp. when I was adding two variables passed as arguments to a function...

View Tutorial          By: itan at 2010-05-04 01:30:24
2. View Comment

Typecasting concept helped me alot in solving my prblem. My problem is when I was trying add two numbers which are passing from an HTML page these two numbers will be acting as strings but my aim is to find out their sum. After using this concept i got my required result. Thank you so much.........

View Tutorial          By: Arun at 2010-09-02 00:23:38
3. View Comment

thanks ..it was really helpful.

View Tutorial          By: jay at 2012-03-19 04:17:14
4. View Comment

Thank you very much.

View Tutorial          By: allcracked at 2013-09-11 19:10:20

Your name (required):


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


Your sites URL (optional):


Your comments:



More Tutorials by Emiley J.
Error generating R.java from manifest
ERROR 1251: Client does not support authentication protocol requested by server; consider upgrading MySQL client
IIS and Tomcat - how to configure to work together
PHP Warning: Unknown(): Unable to load dynamic library '/usr/local/php4/lib/php/extensions/no-debug ......
Adding your own Application icon for your J2ME application (jar file)
Creating Menus in Code using VB.net
Traversing all files in a directory using Ruby
Using Proxy to connect to URLs in Ruby
POST a form in Ruby
Reading URL content using Ruby (HTTP)
dRuby client/server mode sample program
Benchmark module in Ruby
encoding and decoding in Ruby using Base64 Module
Module Abbrev in Ruby
Standard Library Packages in Ruby

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 )
Typecasting 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
List of Keywords and reverved words in Javascript
Scrolling message on the status bar using Javascript
Getting Browser's height and width using Javascript
Javascript Basics
History and evolution of Javascript
do-while, while, for and for-in loops in Javascript
call() and apply() methods in Javascript
Pause execution in JavaScript
Bitwise NOT, AND, OR, XOR operators in Javascript
Using ! and !! in Javascript
The if statement in Javascript
switch in Javascript
Most Emailed Articles (in Javascript)
Click to remove an element from a select html form using javascript
Bitwise NOT, AND, OR, XOR operators in Javascript
this keyword sample in Javascript
Diabling Right Click option in a browser using Javascript
Getting Browser's height and width using Javascript
Show how many characters remaining in a html text box using javascript
History and evolution of Javascript
Using toString() in JavaScript to convert data types to String
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Using ! and !! in Javascript
Using && in Javascript
Highlighting text on a page using CSS
Using revealTrans to do page transitions in Javascript
Javascript to display client date and time on webpage
Verifying user input in JavaScript