PDF In-Depth

Typecasting in JavaScript

June 01, 2001

Advertisement
Advertisement
 

I may have alluded to this before, in various examples, but I want to make it clear again just in case you missed it. In JavaScript, you sometimes find yourself in a situation where you explicitly need to cast a variable to a Number or a String. Many oldtimers are used to multiplying variables by 1.0 (or adding zero) in order to "coerce" them to a numeric value, and of course it's common practice to append toString() to a variable to make it behave as a string. But there's an even cleaner way. Shroud the variable in Number() or String(). For example:


var n = 30; // number 
var m = 70; // number
var s = new String();// "3070"
s = String(n) + String(m);
// "100"
s = Number(n) + Number(m);

Now try to guess what the outcome is if you try Number(n) + String(m). If you've done very much JavaScript coding, you know the answer: The combination Number(n) + String(m) produces a string, "3070".

If you've followed everything thus far, you may be interested to know that you can temporarily cast a variable to a Boolean value with Boolean(). For example:

var n = 50; // number

var b = Boolean(n); // b is "true"
var c = Boolean(n - 50); // c is "false"

For extra credit, try figuring out what the following Boolean code evaluates to:

var s = new String(); // null string

var b = Boolean(s); // what is it?

The answer may surprise you. A null string evaluates to true. Unless you shroud it in Number(), in which case it is false.

Related Products at PDF Store

Nitro PDF Professional OCR

Nitro PDF Professional OCR gives you all the features of Nitro Pro, plus the added power of Optical ... View full product details
Download free demo

Nitro PDF Professional

Nitro PDF Professional, your PDF creation and editing product. Priced at $99.99, Nitro PDF Pro is th... View full product details
Download free demo

XpdfViewer

This ActiveX control (OCX) provides a PDF file viewer component, enabling developers to add PDF view... View full product details
Download free demo

PDF In-Depth Free Product Trials Ubiquitous PDF

Nitro PDF Professional

the perfect PDF product for business and enterprise, combining an extremely competitive price with a...

Download free demo

XpdfViewer

This ActiveX control (OCX) provides a PDF file viewer component, enabling developers to add PDF viewing...

Download free demo

Ubiquitous PDF: Planning for unexpected cash

It's the end of the financial year and some lucky souls are expecting a tax return. Whether or not the dollars are stacking up for you, it's worth keeping in mind this new PDF tool from Squawkfox.

July 29, 2010
Search Planet PDF
more searching options...







Download PDF Creator

Download The Best of Planet PDF volume 2
Planet PDF Newsletter
Most Popluar Articles
Features

Collating PDFs using JavaScript

Despite the numerous benefits, there can be potential issues with the conversion of paper documents into electronic archives. When scanning paper pages into PDF, it's possible to end up with the odd- and even-numbered pages in separate PDF files. It can be very time-consuming to collate them manually, but there is an easier way. Sean Stewart explains.

Featured Product

BCL easyPDF SDK

BCL easyPDF SDK is a set of PDF Programming Libraries designed specifically to help Software Developers / Programmers build and deploy enterprise class PDF applications for corporate wide PDF...

Platinum Sponsor
Create & Edit PDF - Nitro PDF Software

ARTS PDF

Silver Sponsors

PDF-Tools QuickPDF: The Unrivaled PDF Developer Toolkit