Martin Heller
Contributing Writer

Coding Tip: JavaScript isn’t Java, C++, or C#, No Matter How it Looks

analysis
Dec 12, 20071 min

This memo just crossed my desk, from the CTO of one of the companies for which I consult. Caveat coder. Looking at some of our JavaScript code it seems like the use of new Boolean() and new String() has been growing. This will get you into trouble.  For example: var b1 = new Boolean(false);var b2 = false; if ( b1 ) {   // How the heck did I get here if x is false?   // Because Boolean ob

This memo just crossed my desk, from the CTO of one of the companies for which I consult. Caveat coder.

Looking at some of our JavaScript code it seems like the use of new Boolean() and new String() has been growing. This will get you into trouble.  For example:

var b1 = new Boolean(false); var b2 = false;

if ( b1 ) {    // How the heck did I get here if x is false?    // Because Boolean objects are true in a logical context. } if ( b2 ) {    // As expected, we don’t execute this code. } var s1 = new String(“”); var s2 = “”;

if ( s1 ) {    // An empty string is supposed to be logically false too!    // But this is a String *object* so we get true. } if ( s2 ) {    // We never run this code, which is correct. }

There are almost no reasons to use the new operator to create a String, Boolean, or Number object, and plenty of reasons why you shouldn’t. If you put any of those into a Session variable it can cause strange errors as well.

If you need to explicitly convert some type to another type, use its conversion operator:

var str = String(num); var num = parseInt(string);

Martin Heller

Martin Heller is a contributing writer at InfoWorld. Formerly a web and Windows programming consultant, he developed databases, software, and websites from his office in Andover, Massachusetts, from 1986 to 2010. From 2010 to August of 2012, Martin was vice president of technology and education at Alpha Software. From March 2013 to January 2014, he was chairman of Tubifi, maker of a cloud-based video editor, having previously served as CEO.

Martin is the author or co-author of nearly a dozen PC software packages and half a dozen Web applications. He is also the author of several books on Windows programming. As a consultant, Martin has worked with companies of all sizes to design, develop, improve, and/or debug Windows, web, and database applications, and has performed strategic business consulting for high-tech corporations ranging from tiny to Fortune 100 and from local to multinational.

Martin’s specialties include programming languages C++, Python, C#, JavaScript, and SQL, and databases PostgreSQL, MySQL, Microsoft SQL Server, Oracle Database, Google Cloud Spanner, CockroachDB, MongoDB, Cassandra, and Couchbase. He writes about software development, data management, analytics, AI, and machine learning, contributing technology analyses, explainers, how-to articles, and hands-on reviews of software development tools, data platforms, AI models, machine learning libraries, and much more.

More from this author