-2

READ THE EDIT SECTION, THE QUESTION IS NOT VALID

The Array object has a reduce and other methods, where as [] does not. So both Array and [] are partially different types(Is types the right word to use since javascript has no types).

var a = [];
var b = new Array();

If object b is Array what should object a be denoted(is denoted the right word?) as.

EDIT:

Apologies for relying on the autocomplete of node console to get to the incorrrect conclusion that "[]" does not have reduce method.

I typed Array.prototype.red<tab> lists reduce method.

Where as [].red<tab> did not list reduce method.

Please vote to close this question.

Community
  • 1
  • 1
Talespin_Kit
  • 20,830
  • 29
  • 89
  • 135
  • 2
    Type this in your console (F12) : `[].reduce` – Denys Séguret Jun 19 '14 at 18:21
  • Take a look here http://www.w3schools.com/js/js_arrays.asp – Alex Char Jun 19 '14 at 18:22
  • 1
    *Why* do you think arrays expressed as literals (`[]`) don't have a `reduce` method? This question is not entirely rhetorical -- what observed behavior or other written source made you think that? Knowing the answer to that question might help us clear us any underlying misunderstanding you have. – apsillers Jun 19 '14 at 18:24
  • 4
    Why was this question reopened ? Event the author asks to close it... – Denys Séguret Jun 19 '14 at 18:44

2 Answers2

2

Actually, they're the same, but there's a special case for when only a single integer is given as parameter to new Array.

var arrayBrackets = [1, 2, 3],
    arrayWithNew = new Array(1, 2, 3),
    arrayWithNewSingle = new Array(3)

console.log(arrayBrackets)      // [1, 2, 3]
console.log(arrayWithNew)       // [1, 2, 3]
console.log(arrayWithNewSingle) // [undefined, undefined, undefined]

See the Mozilla Developer Network for Array.

Aeveus
  • 5,052
  • 3
  • 30
  • 42
  • is there any way to methodically create [3] (without a literal) ? – dandavis Jun 19 '14 at 18:38
  • 1
    No, there isn't. Although I would advise you to always use the bracket syntax. Someone might pollute the global scope with his own `function Array()`, which might leave your application crippled. – Aeveus Jun 19 '14 at 18:42
1

Some data types in Javascript have primitive forms, like strings and numbers. The primitives and the corresponding objects are not the same, but when you apply a method to a primitive that needs it to be an object, the primitive value is automatically converted to an object. A number like 42 is just a numeric value, but you can use (42).toString() any time, which will automatically create a Number object that the method can be applied to.

Arrays however doesn't have primitive values. The result of the array literal [] is an array object just like the one returned from new Array();. Both are complete objects from the start, there is no difference at all in the result from the two ways of creating an array.


There is a difference in the use of the two way to create arrays, though. If you use the array constructor with a single numeric parameter it will create an array with that length, otherwise it creates an array with the items that you call it with. Also you can omit items in an object literal, like [,,,1,2,,3,,], which will create undefined items in the array.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 2
    `42.toString()` is a syntax error. I think you wanted `42..toString()` ;) – rlemon Jun 19 '14 at 18:49
  • @rlemon: You are right, the numeric syntax conflicts with the method call. I changed it to `(42).toString()` as I think that is easier to grasp. – Guffa Jun 19 '14 at 18:57