concat vs join

+1 Faisal Hamidi · December 30, 2014
can we use join instead of concat if we can than what is the difference?
thank you

Post a Reply

Replies

Oldest  Newest  Rating
+3 Jay Tauron · December 30, 2014
.join() will join the elements of an array into a string separated by the argument, e.g.
x = [1, 2, 3, 4, 5];
x.join("a");
// "1a2a3a4a5"

.concat() will just add the argument to the end of the string or array, e.g.
x = [1, 2, 3, 4, 5];
x.concat("a");
// [1, 2, 3, 4, 5, "a"]
0 Jay Deshaun · December 30, 2014
That's exactly the difference between those 2.
0 Faisal Hamidi · December 31, 2014
thank u guys :)
0 Sochima Biereagu · January 10, 2015
.join() converts an array into a string and seperates it with the argument you pass.

var arr = new Array("hello", "world");
arr.join("+"); //=> "hello+world"

while the .concat() method concatentaes an array, joins the argument(s) you pass in to the array

var arr = new Array("hello");
ar.concat("world"); //=>["hello","world"]
  • 1

Javascript

103,650 followers
About

A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.

Links
Moderators