Home > Web Front-end > JS Tutorial > How to combine arrays and strings using the concat method in JavaScript

How to combine arrays and strings using the concat method in JavaScript

不言
Release: 2019-01-19 16:42:41
Original
2796 people have browsed it

The concat method is used to combine two or more arrays to create a new array. In this article, we will take a look at the specific usage of the concat method in How to combine arrays and strings using the concat method in JavaScript.

How to combine arrays and strings using the concat method in JavaScript

Combining arrays using the concat method

Combining two arrays

Here we introduce how to use the concat method to combine two arrays.

Let’s look at the following example

var array1 = ['ha', 'la'];
var array2 = ['re', 'he'];
var result = array1.concat(array2);
console.log(result);
Copy after login

The execution result is as follows

ha,la,re,he
Copy after login

Through the above method, we can combine two arrays together

Combination Three arrays

The code is as follows

var array1 = ['ha', 'la'];
var array2 = ['re', 'he'];
var array3 = ['jo', 'ko'];
var result = array1.concat(array2, array3);
console.log(result);
Copy after login

The execution result is as follows

ha,la,re,he,jo,ko
Copy after login

Through the above method, we can combine the three arrays together.

Use the concat method to combine strings

The concat method can also combine strings together. The basic usage is the same as combining arrays

Let’s look at a specific example

The code is as follows

var str1 = 'php';
var str2 = '中文网';
var result = str1.concat(str2);
console.log(result);
Copy after login

The execution result is as follows

php中文网
Copy after login

In the above example, you can see that two strings are combined together and output is a string.

When combining large numbers of strings, execution performance (speed) must also be considered.

In fact, the fastest and easiest way to implement string combination is to use " ".

This article ends here. For more exciting content, you can pay attention to the relevant column tutorials on the PHP Chinese website! ! !

The above is the detailed content of How to combine arrays and strings using the concat method in JavaScript. 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