New Array and join () Javascript Method

Inspired by this popular speech, I wanted to find out what the problem is with creating arrays. Let's say I create a new array using

Array(3)

In the console, I get:

[undefined, undefined, undefined]

This is pretty obvious. Say I'm doing a join to this array:

Array(3).join()

As an answer, I get:

",,"

This is also understandable, because these are three empty lines, separated by commas, I suppose. But when I try to do:

Array(3).join("lorem")

I get a line with only two lorem repeats:

"loremlorem"

Why are there two, not three repetitions of this word?

+5
source share
5 answers

joinconnects the elements together using what was transferred as a joiner. So you have three empty lines surrounding lorems:

|lorem|lorem|

, :

var arr = [1, 2, 3, 4, 5]; // Like Array(5), except not sparse

arr.join('-and-'); // 1-and-2-and-3-and-4-and-5

join , , . ,, ",,". ( .)

+8

Join . "lorem" , .

+3

.

, 3 , 2 ( 1- 2-, 2- 3-).

var a = [1,2,3];
a.join(','); //1,2,3
a.join('test'); // 1test2test3
+2

join .

, join, . Array(3), . join , "".

, : blank lorem blank lorem blank. - .

:

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

bananaloremorangeloremapple

+2

Array 3 , .join , .

.

.join(), .

.join() ",", :

new Array(3).join();

:

",,"

, :

[, ,]

new Array(3) console .join().

+1

All Articles