-1

I have come across an interesting piece of code:

function repeat(str,x) {
    return Array(x+1).join(str); 
}
repeat("wow", 2);

The outcome of this is a string "wowwow". However, I have no idea what this Array(x+1) is actually doing. And very interesting thing is that if I just use Array(x) it prints the str only once and not twice as expected.

When I console.log Array(x+1) it gives this strange output:

enter image description here

Array(x+1) (3) [empty × 3]

I am aware that there exists a repeat() method on strings which can be used happily to achieve the same result as the presented function. But as I've come across it, I would like to know the mechanism behind Array(x+1). I also know what an array or new Array() is. But this I see for the first time.

bakrall
  • 465
  • 7
  • 21
  • please paste output as text, not image. – Adrien Brunelat Jan 18 '18 at 12:56
  • `Array`, even without new, creates a new instance. It's the same with or without it. If you create an array with a number as the first argument, a new array with said *length* but no elements is created. – Andrew Li Jan 18 '18 at 12:56
  • Possible duplicate of [What is the 'new' keyword in JavaScript?](https://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript) – kLabz Jan 18 '18 at 12:57

5 Answers5

4

Array is specified such that new is optional. From the spec:

When called as a constructor it creates and initializes a new Array exotic object. When Array is called as a function rather than as a constructor, it also creates and initializes a new Array object. Thus the function call Array(…) is equivalent to the object creation expression new Array(…) with the same arguments.

(my emphasis)

To my surprise, the MDN page is not at all clear about this. If I get time, I may have to fix that...

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • @T.J. Crowder, exactly. I was checking MDN because it is my usual place for reference and I could not get it from there. I am glad to know now the other source you gave. – bakrall Jan 18 '18 at 13:21
2

As you can see in console Array(x+1) creates array with 3 empty elements (as far as x = 2). Then you join these elements with string wow so you have:

empty element + "wow" + empty element + "wow" + empty element = "wowwow"

Arkej
  • 2,221
  • 1
  • 14
  • 21
0

Array(x+1) creates an array of 3 elements all containing empty elements. Later you are joining these empty elements with the string wow, thus returning: empty + "wow" + empty + "wow" + empty => wowwow

Jp Mohan
  • 160
  • 2
  • 7
0

It is a function that creates an array. Things like this you should really just try in you console before asking.

These things can be done in Javascript even though they are somewhat contra intuitive:

function A() {
    return new Array();
}
let myA1 = A(); // returns an array
let myA2 = new A(); // also returns an array.

EDIT: The reason is that Javascript is a prototypal language and a "Class" is just a function that we new. If that function is not returning anything we get a new instance of in our case A and if the function returns some other object, that object gets returned.

JGoodgive
  • 1,068
  • 10
  • 20
0

What is does is to create an array with size x+1(3, in this example). And filling it each cell with the str variable value.

In this code I create an array of size 3 cells and fill them with the string "yolo".

$(document).ready(function(){
 var str = "yolo";

 console.log(Array(4).join(str));  
})

Example fiddle: https://jsfiddle.net/6o90fv9c/

Hiplobbe
  • 138
  • 6