Home >Web Front-end >JS Tutorial >How to use join method
The Array.join() function is used to join the elements of the array into a string. Let's take a closer look at the specific usage of the Array.join() function.
Let’s first take a look at the basic syntax of the Array.join() function
Array.join([separator])
separator represents each element used to separate the array. A string of elements. If keeping the default array elements, separate them with commas (,).
This function returns a string created by concatenating all elements of the array using delimiters. If no delimiter is provided, the array elements are concatenated using comma (,) as it is the default delimiter for this function. If an empty string is provided as the delimiter, the elements will be concatenated directly without any characters between them. If array is empty, this function returns an empty string.
Let’s look at specific examples
Example 1:
The code is as follows
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <script> function func() { var a = [ 1, 2, 3, 4, 5, 6 ]; document.write(a.join('|')); } func(); </script> </body> </html>
The output results are as follows: 1|2|3| 4|5|6
Example 2:
The code is as follows
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <script> function func() { var a = [ 1, 2, 3, 4, 5, 6 ]; document.write(a.join()); } func(); </script> </body> </html>
The output result is as follows: 1,2,3,4,5,6
Example 3:
The code is as follows
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <script> function func() { var a = [ 1, 2, 3, 4, 5, 6 ]; document.write(a.join('')); } func(); </script> </body> </html>
The output result is as follows: 123456
This article has ended here, you can pay attention to more exciting content Other related column tutorials on php Chinese website! ! !
The above is the detailed content of How to use join method. For more information, please follow other related articles on the PHP Chinese website!