Three JavaScript Quirks That Java/C Developers Should Know

Many of the JavaScript built in functions/objects make use of the difference between calling them using new and calling them without.

For example:

today1 = Date();
today2 = (new Date()).toLocaleString();

the first of these is calling the Date() function to return today’s date in whatever format your browser is set to use while the second of these creates a Date() object and then uses a method on that object to return today’s date in whatever format your browser is set to use.

Similar function/object pairs exist for Boolean() Number() and String() where the function converts whatever is passed to it to the appropriate primitive data type while the object call (with new) creates an object of that type (which is not the same type as the primitive that the function returns).