[JavaScript/ES6] How to Use Single Quotes, Double Quotes, and Backticks
I was asked by a member in engineer training about how to use single quotes, double quotes, and backticks in JavaScript (ECMAScript 6: ES6), so I’ll introduce this topic with reference to MDN articles.
文字列リテラルとは、0 個以上の文字を二重引用符 (") または単一引用符 (') でくくったものです。文字列は同じ種類の引用符でくくらなければなりません。つまり、どちらも単一引用符にするか、またはどちらも二重引用符にします。“A string literal is zero or more characters enclosed in double (”) or single quotation marks (’). A string must be delimited by quotation marks of the same type; that is, either both single quotation marks or both double quotation marks.”
let singleStr = 'He said "Hello".';
console.log(singleStr);
// He said "Hello".
let doubleStr = "I'm fine.";
console.log(doubleStr);
// I'm fine.
const DOMAIN_NAME = 'codenote.net';
let backquoteStr = `This site is ${DOMAIN_NAME}.`;
console.log(backquoteStr);
// This site is codenote.net.
This is my personal summary, but I develop with a policy of using “single quotes ’” for string literals and “backticks `” for template strings.
The reason is that according to the article Should I use ‘single’ or “double-quotes” for strings in JavaScript, many famous JavaScript OSS projects adopt single quotes ’.
That’s all from the Gemba.