Careful about using wrapper types in JavaScript

We are aware that JavaScript provides 5 Primitive Types, namely string,  boolean, number, undefined and  null. These types except null and undefined have their own wrappers that create an object version  of  these types.

I will use String type to illustrate my point above.

We create a primitive string…

    var strPrimitive = "Hello World"   // Primitive string

and its object equivalent..

    var strObj = new String("Hello World"); // Object hash of the string

So, we might assume it is fine to use any of the Primitive or its Wrapper type in our code. But, be aware of the following pitfalls,

  • Observe that, the primitive and its wrapper types aren’t the same for obvious reasons (Primitives => string literal; Wrapper => string object)

     strPrimitive == strObj // false
     strPrimitive === strObj // false.

  • Also, JavaScript auto converts primitive to object as and when necessary and vice-versa. This perhaps explains why all the methods on the String prototype are available to the primitives.
  • Using the primitive types also saves some memory on Heap, which otherwise have to be allocated for the objects.

Using wrapper types is also discouraged for the following scenarios, particularly with Boolean type.

A simple boolean primitive would look like:

   var bool = false;

This works good in our code while doing something like this:

   if(bool){ // Do something if true...}

But consider what would happen if you were to use the wrapper instead of the primitive.

   var bool = new Boolean(false);

Now, doing so would prove to be fatal!, as the below code would evaluate to be true since objects are truthy.

   if(bool){ // Do something if true...}

So, the correct way to check would be using valueOf;

   if(bool.valueOf()){ // Do something if true...}

In all, it is best to avoid using wrappers when we can accomplish the same using the simple primitives.

Understanding JSON (JavaScript Object Notation)

JSON or the JavaScript Object Notation, as we know, is one of the highly popular data-interchange format along with XML, YAML, RDF etc. Its popularity is mainly due to its syntactical similarities with the JavaScript and it being human readable. Often, we find it difficult to clearly distinguish between JSON and its JavaScript counterpart – A simple JavaScript Object. In this article, I will try to distinguish the fundamental differences between the two.

Firstly, let me define a simple literal object in JavaScript. It looks like this, in its simplest form:-

var jsObj =

   {  
       "firstName" : "John" ,
       "lastName" : "Michael"
   };

Continue reading

Most useful tools for a web developer

“Give me six hours to chop down a tree and I will spend the first four sharpening the axe” – Abraham Lincoln

Likewise, any web developer needs to be aware of the tools which aids in faster and better web development. In this article, I wish to introduce few tools which I found to be immensely helpful for any budding web developer to have it in their arsenal.

  • Editors :- To start with, a developer needs an editor, a tool in which development happens and where the developer spends most part of the time. So, it is only wise that developers choose the right editor. Although, it takes time to get comfortable using any editor, it is worth the effort to try out different editors and choose one which is helpful the most. The choices vary from developers to developers and it is best left to the individual to make a choice.

I started with Notepad++ but eventually found Sublime Text to be the one suitable for me. These two seems to be the most popular choices, but I have seen my friends use other alternatives such as Eclipse and Aptana, which are popular too.

Continue reading

JavaScript – Simple, Intuitive & Promising

Web development is an area of immense interest to me. Having been in this field for over 3 years now, I look back at the time I ventured into this field for the first time during my college days for some academic project. As with any newbie, I started with HTML and CSS with an intention to design a simple login page. Designing a “Login Page” is akin to writing a “Hello World” program.

It started off pretty well and I was able to get the login page designed in no time. Once I got the hold on HTML & CSS, my curiosity increased manifold and now I wanted to add a “Home Page” which is to be shown once the login details are successfully authenticated. I was hence introduced to JavaScript and backend programming with PHP.

Continue reading