如何在JavaScript中连接两个字符串

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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值