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

How to use javascript join method

藏色散人
Release: 2021-03-30 15:07:07
Original
6118 people have browsed it

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)
Copy after login

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>
Copy after login

Output:

George,John,Thomas
Copy after login

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>
Copy after login

Output:

George.John.Thomas
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template