javascript interactive

JavaScript Interactive — a console crash course


4.7 Function & Array Objects

Warning: Your browser may be outdated or unsupported.

The JavaScript console is known to display some of the examples incorrectly in Internet Explorer 8 or older.
>>>
typeof (new Function())
"function"
Although the Boolean, Number and String constructors create wrapper objects of little practical value, the Function constructor does not.
>>>
(function (){}) instanceof Function
true
Instead, function objects and function values are the same thing. Which is to say, not really an object type at all.
>>>
(new Function('return 1+2;'))()
3
The Function constructor is also somewhat useful, as it allows creating anonymous functions from a string (containing JavaScript code).
>>>
new Array(1,2,3)
[1, 2, 3]
For Array instances, the constructor function is mostly equivalent to using the literal syntax (JSON).
>>>
new Array(3)
[undefined, undefined, undefined]
The only exception is when a single numeric argument is used. In this case, the result is an empty array of the specified length. Using the Array constructor is therefore discouraged, as the literal syntax is less error-prone and more compact.
>>>
new Object()
{}
Similar to Array, the Object constructor should also be avoided in most cases. Using the {} syntax is both clearer, more compact and more flexible.
>>>
Array(3)
[undefined, undefined, undefined]
All the built-in constructor functions can also be used without new, producing the same results. This isn't automatically true for other constructor functions, however.
>>>

Index 1 2 3 4 5 6 7 8