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.
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);
The execution result is as follows
ha,la,re,he
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);
The execution result is as follows
ha,la,re,he,jo,ko
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);
The execution result is as follows
php中文网
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!