handling double quotes and single quotes in javascript code example
Example: javascript string single or double quotes
/*
You can use either.
Double quotes are the only allowed character for
strings in JSON, and HTML conventions suggest using it.
However, single quotes are far more popular in JS and CSS.
Most JS learning programs use the single quote, but most development
environments mainly link to or even have all of their templates in
double quotes, as well. Although I've learned JS with
single quotes and it seems archaic in some places, I discourage,
denounce, and despise not using single quotes in programs
that don't have all of their templates mostly in double
quotes. They've become a popular choice, although not widespread.
Also, if you have access to ES6, you can also use a backtick.
Although it is scarcely used other than using it for its actual
purpose, which is a template literal, it'll also save you from
typing some backslashes, and it only requires a tiny bit
of practice to type easily.
*/
// Example 1
'Please don\'t try this at home. We\'re professionally trained to do this.'
"Please don't try this at home. We're professionally trained to do this."
`Please don't try this at home. We're professionally trained to do this.`
// Example 2
'"To live is to struggle. To survive is to find meaning in the struggle."'
"\"To live is to struggle. To survive is to find meaning in the struggle.\""
`"To live is to struggle. To survive is to find meaning in the struggle."`
// Example 3
'`const meaningOfLife = 42;`'
"`const meaningOfLife = 42;`"
`\`const meaningOfLife = 42;\``
// Example 4
'"We\'re no strangers to love."'
"\"We're no strangers to love.\""
`"We're no strangers to love."`
// Example 5
'`console.log("Hello World!");`'
"`console.log(\"Hello World!\");`"
`\`console.log("Hello World!");\``
// Example 6
'`console.log(\'Hello World!\');`'
"`console.log('Hello World!');`"
`\`console.log('Hello World!');\``
// Example 7
'Which? `console.log("Hello World!");`\n`console.log(\'Hello World!\');`'
"Which? `console.log(\"Hello World!\");`\n`console.log('Hello World!');`"
`Which? \`console.log("Hello World!");\`\n\`console.log('Hello World!');\``
/*
Here's the amount of examples which required escape characters:
Single quote: 1, 4, 6, 7 (4).
Double quote: 2, 4, 5, 7 (4).
Backtick: 3, 5, 6, 7 (4).
They're all even. Go and look back at the examples, and pick the
character that won't require escape characters in your most common
use cases.
*/
/*
The conclusion? I'm tending toward backticks right now.
Of course, I'm still not undecided about using single quotes
around mostly, but, practically, you'd wanna use backticks.
*/