JavaScript, like any good language, has the ability to join 2 (or more, of course) strings.
JavaScript,就像任何一种好的语言一样,具有连接2个(或更多)字符串的能力。
How?
怎么样?
We can use the +
operator.
我们可以使用+
运算符。
If you have a string name
and a string surname
, you can assign those too the fullname
variable like this:
如果您有一个字符串name
和一个字符串surname
,则也可以为它们分配fullname
变量,如下所示:
const fullname = name + surname
If you don’t want to instantiate a new variable, you can use the +=
operator to add the second string to the first:
如果您不想实例化新变量,则可以使用+=
运算符将第二个字符串添加到第一个字符串:
name += surname
Alternatively you can also use the concat()
method of the String object, which returns a new string concatenating the one you call this method on, with the argument of the method:
另外,您也可以使用String对象的concat()
方法,该方法返回一个新字符串,该字符串将您调用此方法的字符串与方法的参数连接在一起:
const fullname = name.concat(surname)
I generally recommend the simplest route (which also happen to be the faster) which is using the +
(or +=
) operator.
我通常建议使用+
(或+=
)运算符的最简单的路由(也可能是更快的路由)。
翻译自: https://flaviocopes.com/how-to-join-strings-javascript/