Home  >  Article  >  Web Front-end  >  How to use javascript join method

How to use javascript join method

藏色散人
藏色散人Original
2021-03-30 15:07:076062browse

The javascript join method is used to put all elements in the array into a string. The syntax of this method is "arrayObject.join(separator)", and its parameter separator represents the separator to be used.

How to use javascript join method

The operating environment of this article: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

JavaScript join() method

Definition and usage

The join() method is used to join all elements in the array Put in a string.

Elements are separated by the specified delimiter.

Syntax

arrayObject.join(separator)

Parameters

separator Optional. Specify the delimiter to use. If this parameter is omitted, a comma is used as the delimiter.

Return value

Returns a string. The string is generated by converting each element of the arrayObject to a string and then concatenating the strings, inserting the separator string between the two elements.

Example

Example 1

In this example, we will create an array and then put all its elements into a string:

<script type="text/javascript">
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr.join())
</script>

Output:

George,John,Thomas

Example 2

In this example, we will use the delimiter to separate the elements in the array:

<script type="text/javascript">
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr.join("."))
</script>

Output:

George.John.Thomas

[Recommended learning: js basic tutorial]

The above is the detailed content of How to use javascript join method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn