Javascript new Array and join() method

后端 未结 5 1787
北恋
北恋 2021-01-13 00:49

Inspired by this popular speech I wanted to figure out some issue related to creating arrays. Let\'s say I am creating new array with:

Array(3)

5条回答
  •  再見小時候
    2021-01-13 01:44

    Have a look to the join documentation.

    What you're passing to the join function will be used as separator between the elements of the array. When you declare an array with Array(3), you're creating an array with three elements. The join method inserts your separator between those elements and so you will see only two "lorem".

    Actually, what you see is: blank lorem blank lorem blank. Where blank is the empty elements of the array.

    Try to do the following:

    var fruits = ["banana", "orange", "apple"]
    fruits.join("lorem")
    

    It will print

    bananaloremorangeloremapple

提交回复
热议问题