# JavaScript 连接两个字符串

**URL:** <https://www.isharkfly.com/t/javascript/8508>\
**Category:** 前端\
**Created:** [October 23, 2012, 1:42am UTC](https://www.isharkfly.com/t/javascript/8508 "2012-10-23T01:42:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [October 23, 2012, 1:42am UTC](https://www.isharkfly.com/t/javascript/8508/1 "2012-10-23T01:42:06Z")

</div>

String 对象的concat( )方法能将作为参数传入的字符串加入到调用该方法的字符串的末尾并将结果返回给新的字符串，语法如下：`newString=targetString.concat(anotherString);`连接字符串的核心语句：`var finalString=targetString.concat(strToBeAdded);`该代码运行后，将字符串strToBeAdded 添加到字符串targetString 的后面，并将生成的新字符串赋值给finalString，连接过程并不改变字符串strToBeAdded 和targetString 的值。

JavaScript 脚本中，也可通过如下的方法实现同样的功能：`var finalString="Welcome to "+"the world!";
var finalString="Welcome to ".concat("the world!");
var finalString="Welcome to ".concat("the ","world!");`String 对象的concat( )方法可接受任意数目的参数字符串，并按顺序将它们连接起来添加到调用该方法的字符串后面，并将结果返回给新字符串。
