javascript单引号_JavaScript中的引号

javascript单引号

JavaScript allows you to use 3 types of quotes:

JavaScript允许您使用3种引号:

  • single quotes

    单引号
  • double quotes

    双引号
  • backticks

    反引号

The first 2 are essentially the same:

前两个基本相同:

const test = 'test'
const bike = "bike"

There’s little to no difference in using one or the other. The only difference lies in having to escape the quote character you use to delimit the string:

使用任何一个几乎没有差异。 唯一的区别在于必须转义用于定界字符串的引号字符:

const test = 'test'
const test = 'te\'st'
const test = 'te"st'
const test = "te\"st"
const test = "te'st"

There are various style guides that recommend always using one style vs the other.

有多种样式指南建议始终使用一种样式,而不是另一种样式。

I personally prefer single quotes all the time, and use double quotes only in HTML.

我个人更喜欢一直使用单引号,并且仅在HTML中使用双引号。

Backticks are a recent addition to JavaScript, since they were introduced with ES6 in 2015.

自2015年在ES6中引入反引号以来,反引号是JavaScript的最新成员。

They have a unique feature: they allow multiline strings.

它们具有独特的功能:它们允许多行字符串。

Multiline strings are also possible using regular strings, using escape characters:

多行字符串也可以使用常规字符串,并使用转义字符:

const multilineString = 'A string\non multiple lines'

Using backticks, you can avoid using an escape character:

使用反引号,可以避免使用转义符:

const multilineString = `A string
on multiple lines`

Not just that. You can interpolate variables using the ${} syntax:

不仅如此。 您可以使用${}语法插值变量:

const multilineString = `A string
on ${1+1} lines`

I cover backticks-powered strings (called template literals) in a separate article, that dives more into the nitty-gritty details.

我将在另一篇文章中介绍反引号驱动的字符串(称为模板文字),该文章将更深入地探讨细节。

翻译自: https://flaviocopes.com/javascript-quotes/

javascript单引号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值