JS: Join() Method

The <span style="color:#a30500" class="tadv-color">join()</span> method from string class, join the characters or words of a string.

Advertisements

This method is useful in some operations where we have to split the string into an array. The<span style="color:#a30500" class="tadv-color"> join()</span> will concatenate it into a string again.

The example program for join operation is given below.

Advertisements
<!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 <span style="color:#a30000" class="tadv-color">join(' '); </span>

You must add more white space in<span style="color:#a30000" class="tadv-color"> join()</span> 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
Advertisements

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version