join() method from string class, join the characters or words of a string.
This method is useful in some operations where we have to split the string into an array. The
join() will concatenate it into a string again.
The example program for join operation is given below.
<!DOCTYPE html> <html> <head> <title>JS: join() Method</title> <meta charset="utf-8"> </head> <body> <script> var name = "woodchuck"; //split the string into a character array var split_name = name.split(''); //join the characters together to form string var out = split_name.join(''); console.log(split_name); //output = ['w','o','o','d','c','h','u','c','k'] console.log(out); //output = 'woodchuck' </script> </body> </html>
The above script will join the character only, However, if you want to join words use a
You must add more white space in
join() method. The following code will show the difference.
string = ['h','e','l','l','o','','w','o','r','l','d']; string.join(''); //output = "helloworld" string.join(' '); //output = 'hello world' string.join('='); //output = h=e=l=l=o=w=o=r=l=d