console.log("double");
VSconsole.log('single');
在处理字符串时,我看到越来越多的JavaScript库使用单引号。使用其中一种而不是另一种的理由是什么?我以为它们是可以互换的。
发布于 2018-08-20 14:10:52
在不同的库中使用单一的和双的最可能的原因是程序员的偏好和/或API的一致性。
除了一致之外,使用最适合字符串的:
使用其他类型的引用作为文字:
alert('Say "Hello"');
alert("Say 'Hello'");
但这会使情况变得复杂
alert("It's \"game\" time.");
alert('It\'s "game" time.');
另一个在ES6中新增的选项是:
alert(`Use "double" and 'single' quotes in the same string`);
alert(`The escape the \` back-tick character in a string`);
模板文字为以下内容提供了一个干净的语法:变量插值,多行字符串等。
发布于 2018-08-20 14:54:26
差异表现在以下几个方面:
'A string that\'s single quoted'
"A string that's double quoted"
https://stackoverflow.com/questions/-100002284
复制相似问题