Hướng dẫn conditional string concatenation javascript - javascript nối chuỗi có điều kiện

Phương thức string.concat() có chức năng nối hai hay nhiều chuỗi lại với nhau thành một chuỗi.

Hướng dẫn conditional string concatenation javascript - javascript nối chuỗi có điều kiện

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.freetuts.net, không được copy dưới mọi hình thức.

Phương thức string.concat() sẽ không làm thay đổi các chuỗi truyền vào và sẽ trả về một chuỗi mới là kết quả của việc nối các chuỗi con truyền vào.

Cú pháp: string.concat(string1, string2, ..., stringX)string.concat(string1, string2, ..., stringX)

Trong đó:

Bài viết này được đăng tại [free tuts .net]

  • string1, string2, ..., stringX là các chuỗi con sẽ được nối với chuỗi string.

Cách sử dụng

Ví dụ: sử dụng phương thức string.concat() để nối các chuỗi.: sử dụng phương thức string.concat() để nối các chuỗi.



    
        
    
    
        

Học lập trình miễn phí tại freetuts.net

Kết quả

Học lập trình miễn phí tại freetuts.net

Tham khảo: w3schools.com

Phương thức concat() dùng để nối hai (hoặc nhiều) chuỗi lại với nhau thành một chuỗi.concat() dùng để nối hai (hoặc nhiều) chuỗi lại với nhau thành một chuỗi.

Cú pháp:

string1.concat(string2, string3, ..., stringN)

Giá trị trả về của phương thức concat() sẽ là một chuỗi được nối bởi các chuỗi: string1, string2, string 3, ...., stringN

Lưu ý: Phương thức concat() không làm thay đổi giá trị của các chuỗi ban đầu, nó chỉ nối các chuỗi ban đầu lại với nhau để tạo ra một chuỗi mới.


Xem ví dụ

The

Học lập trình miễn phí tại freetuts.net
0 method concatenates the string arguments to the calling string and returns a new string.
Học lập trình miễn phí tại freetuts.net
0
method concatenates the string arguments to the calling string and returns a new string.

Try it

Syntax

concat(str1)
concat(str1, str2)
concat(str1, str2, /* …, */ strN)

Parameters

Học lập trình miễn phí tại freetuts.net
1

One or more strings to concatenate to

Học lập trình miễn phí tại freetuts.net
2.

Return value

A new string containing the combined text of the strings provided.

Description

The

Học lập trình miễn phí tại freetuts.net
0 function concatenates the string arguments to the calling string and returns a new string. Changes to the original string or the returned string don't affect the other.

If the arguments are not of the type string, they are converted to string values before concatenating.

The

Học lập trình miễn phí tại freetuts.net
0 method is very similar to the addition/string concatenation operators (
Học lập trình miễn phí tại freetuts.net
5,
Học lập trình miễn phí tại freetuts.net
6), except that
Học lập trình miễn phí tại freetuts.net
0 coerces its arguments directly to strings, while addition coerces its operands to primitives first. For more information, see the reference page for the
Học lập trình miễn phí tại freetuts.net
5 operator.

Examples

Using concat()

The following example combines strings into a new string.

const hello = 'Hello, ';
console.log(hello.concat('Kevin', '. Have a nice day.'));
// Hello, Kevin. Have a nice day.

const greetList = ['Hello', ' ', 'Venkat', '!'];
"".concat(...greetList)  // "Hello Venkat!"

"".concat({})    // [object Object]
"".concat([])    // ""
"".concat(null)  // "null"
"".concat(true)  // "true"
"".concat(4, 5)  // "45"

Specifications

Specification
ECMAScript Language Specification # sec-string.prototype.concat
# sec-string.prototype.concat

Browser compatibility

BCD tables only load in the browser

See also