Many times when you are looking at JavaScript code, you will see the word new within the code. What this is doing is creating a new instance of an object that has been defined by the user or a new instance of a built in object type.


Using 'new' wtih an object constructor

Object constructors are a standard way of creating an object type. You create a function for creating many objects for one type of object. You use new to create a new object type contining these various objects. For example:

function tool(brand, model, cost, quantity, model) {
    this.brandname = brand;
    this.cost = retailprice;
    this.quantity = quantity;
    this.color = color;

var hammer = new tool("BuiltRite", "claw", 6.78, 250);
var drill = new tool("Black and Decker", "variable speed", 28.95, 40);

In this example new allows you to create a new object type and specify the objects within.


Using 'new' with built-in JavaScript Constructors

You can also use new when declaring a variable that will be one of the built-in JavaScript objects. For example:

var abc = new Object();
var bcd = new String();
var cde = new Number();
var def = new Boolean();
var efg = new Array();
var fgh = new RegExp();
var ghi = new Date();

Using new for built-in JavaScript constructors is not necessary, as there are other ways of stating the same idea.The above statements could also be coded as shown below.

var abc = {};
var bcd = "";
var cde = 0;
var def = false;;
var efg = [];
var fgh = /()/;
var ghi = function();