Javascript new Array and join() method

后端 未结 5 1794
北恋
北恋 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:22

    If you have an Array of 3 members, the .join is the filler in between the members, so there should only be two join strings.

    Therefore your second output is correct.

    Your first output using .join(), seems like a display bug or a misrepresentation of your testing code.

    The default value for .join() is a ",", so this:

    new Array(3).join();
    

    should give you this:

    ",,"
    

    The output that you show:

    [, ,]
    

    will more likely come from just typing new Array(3) in the console without the .join().

提交回复
热议问题