The primitive or special reference types in JavaScript are created to ease up the operations with primitive values, such as the Boolean type, the Number type and the String type. In simpler terms, primitive wrappers, although not objects, do have methods that allow the developers to manipulate a primitive value in same manner. For instance:

var myString = "the String type primitive wrapper";

var myStringext = myString.substring(4);

The example above shows a possible usage of the method "substring" which ECMAScript executes in following manner:

  1. Instance of the String type is created;
  2. Method substring() is created;
  3. Instance is destroyed.

Exactly the last part of the execution is the difference between a regular reference type and a primitive wrapper. Other reference types create by using operator new and stay in memory as long as the scope lasts, while primitive wrapper's objects exist for one line of the code only.

Remember that because the instance of a primitive type is destroyed as soon as the first line of its creation ends, new methods cannot be added at runtime. For instance:

var myString = "the String type primitive wrapper";

var myStringext = myString.substring(4);

alert(myString. myStringext);    // undefined

The above alert() instruction will render value type to be undefined because the variable instance had already been destroyed.

 

›› go to examples ››