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.
*/
Also in JavaScript:
- enable disable click on div jquery
- what is foreach method in javascript
- javascript perimeter
- import React, { memo } from 'react';
- fs create or edit file
- angular [routerlink]
- es6 js slug from string
- 00:00:00 / 00:00:00 js
- check for internet explorer browser in javascript
- datatables clear table
- query selector click event
- how to apply reduce to an empty array in javascript
- logical operators javascript
- object to json string android
- linked list javascript
- nuxt error page
- match js
- android center text react native
- how to set view engine in express
- javascript refresh page
- switch/case with numeric range
- how to import js via script in react
- js set visibility on timeout
- get field type file js and loop