本文共 1458 字,大约阅读时间需要 4 分钟。
javascript单引号
JavaScript allows you to use 3 types of quotes:
JavaScript允许您使用3种引号:
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 stringon multiple lines`
Not just that. You can interpolate variables using the ${}
syntax:
不仅如此。 您可以使用${}
语法插值变量:
const multilineString = `A stringon ${1+1} lines`
, that dives more into the nitty-gritty details.
将更深入地探讨细节。
翻译自:
javascript单引号
转载地址:http://jxqgb.baihongyu.com/