Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript new Array and join() method

Tags:

javascript

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)

In console I am getting:

[undefined, undefined, undefined]

Which is pretty obvious. Let's say I am doing joining on that array:

Array(3).join()

As a response I am getting:

",,"

Which is pretty understandable as well, because these are three empty strings, separated by commas, I suppose. But when I am trying to do:

Array(3).join("lorem")

I am getting string with only two repeat of ”lorem”:

"loremlorem"

Why there are two, not three repeats of that word?

like image 916
user1292810 Avatar asked Sep 11 '12 18:09

user1292810


People also ask

How do I combine array elements into strings?

The arr. join() method is used to join the elements of an array into a string. The elements of the string will be separated by a specified separator and its default value is a comma(, ).

How do I combine two arrays?

The concat() method concatenates (joins) two or more arrays. The concat() method returns a new array, containing the joined arrays. The concat() method does not change the existing arrays.

How do you split and join in JavaScript?

How it works: First, split the title string by the space into an array by using the split() string method. Second, concatenate all elements in the result array into a string by using the join() method. Third, convert the result string to lower case by using the toLowerCase() method.

How do you turn an array into a string?

To convert a JavaScript array into a string, you can use the built-in Array method called toString . Keep in mind that the toString method can't be used on an array of objects because it will return [object Object] instead of the actual values.


5 Answers

Join takes a separator. "lorem" has replaced the commas that were there before.

like image 26
jncraton Avatar answered Oct 04 '22 16:10

jncraton


join joins the elements together, using what was passed as a joiner. So you have three empty strings "surrounding" the lorems:

|lorem|lorem|

It might be a little more obvious if you don't use an empty array:

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

And your first example join output, by the way, is incorrect. It should be ,, or ",,". (Depends on the output format.)

like image 95
Ry- Avatar answered Oct 04 '22 17:10

Ry-


Join combines the elements in an array with the specified delimiter.

So, since there are 3 elements, you only need 2 delimiters (between 1st and 2nd, and between 2nd and 3rd).

var a = [1,2,3];
a.join(','); //1,2,3
a.join('test'); // 1test2test3
like image 41
Rocket Hazmat Avatar answered Oct 04 '22 15:10

Rocket Hazmat


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

like image 31
Zagorax Avatar answered Oct 04 '22 16:10

Zagorax


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().

like image 32
gray state is coming Avatar answered Oct 04 '22 16:10

gray state is coming