Difference between single quotes and double quotes in Javascript
You'll want to use single quotes where you want double quotes to appear inside the string (e.g. for html attributes) without having to escape them, or vice versa. Other than that, there is no difference.
However, note that JSON (JavaScript Object Notation) only supports double quoted strings.
When should I use double or single quotes in JavaScript?
The most likely reason for use of single vs. double in different libraries is programmer preference and/or API consistency. Other than being consistent, use whichever best suits the string.
Using the other type of quote as a literal:
alert('Say "Hello"');
alert("Say 'Hello'");
This can get complicated:
alert("It's \"game\" time.");
alert('It\'s "game" time.');
Another option, new in ECMAScript 6, is template literals which use the backtick character:
alert(`Use "double" and 'single' quotes in the same string`);
alert(`Escape the \` back-tick character and the \${ dollar-brace sequence in a string`);
Template literals offer a clean syntax for: variable interpolation, multi-line strings, and more.
Note that JSON is formally specified to use double quotes, which may be worth considering depending on system requirements.
What is the difference between single or double quotes in my JS and CSS?
The most important thing is to be consistent. Use the same quote style throughout the same project. If you enjoy writing JavaScript with single-quotes, please do. But If your teammate likes to use double-quotes, agree on what style to use before working.
Additional information:
- When to use double or single quotes in JavaScript?
- Which type of quotes we should use in css background url ("....")? Single, double or no quote needed?
Double quotes vs single quotes in JavaScript
The difference is that you don't need to escape single quotes in double quotes, or double quotes in single quotes. That is the only difference, if you do not count the fact that you must hold the Shift key to type "
.
What is the difference between single-quoted and double-quoted strings in PHP?
PHP strings can be specified not just in two ways, but in four ways.
- Single quoted strings will display things almost completely "as is." Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash
\'
, and to display a back slash, you can escape it with another backslash\\
(So yes, even single quoted strings are parsed). - Double quote strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated. An important point here is that you can use curly braces to isolate the name of the variable you want evaluated. For example let's say you have the variable
$type
and you want toecho "The $types are"
. That will look for the variable$types
. To get around this useecho "The {$type}s are"
You can put the left brace before or after the dollar sign. Take a look at string parsing to see how to use array variables and such. - Heredoc string syntax works like double quoted strings. It starts with
<<<
. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. You don't need to escape quotes in this syntax. - Nowdoc (since PHP 5.3.0) string syntax works essentially like single quoted strings. The difference is that not even single quotes or backslashes have to be escaped. A nowdoc is identified with the same
<<<
sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g.<<<'EOT'
. No parsing is done in nowdoc.
Notes:
Single quotes inside of single quotes and double quotes inside of double quotes must be escaped:
$string = 'He said "What\'s up?"';
$string = "He said \"What's up?\"";
Speed:
I would not put too much weight on single quotes being faster than double quotes. They probably are faster in certain situations. Here's an article explaining one manner in which single and double quotes are essentially equally fast since PHP 4.3 (Useless Optimizations
toward the bottom, section C
). Also, this benchmarks page has a single vs double quote comparison. Most of the comparisons are the same. There is one comparison where double quotes are slower than single quotes.
Difference between single and double quotes in Flutter/Dart
Single and double quotes both work in Dart
final myString = 'hello';
is the same as
final myString = "hello";
Delimiters need to be escaped
Use a \
backslash to escape single quotes in a single quote string.
final myString = 'Bob\'s dog'; // Bob's dog
Same thing to escape double quotes in a double quote string.
final myString = "a \"quoted\" word"; // a "quoted" word
But no need to escape anything if the delimiter is different.
final myString = "Bob's dog"; // Bob's dog
final myString = 'a "quoted" word'; // a "quoted" word
Also no need to worry about the value passed into an interpolated string.
final value = '"quoted"'; // "quoted"
final myString = "a $value word"; // a "quoted" word
Prefer single quotes in Flutter
The Flutter style guide recommends using single quotes for everything
final myString = 'hello';
except for nested strings
print('Hello ${name.split(" ")[0]}');
or strings containing single quotes (optional)
final myString = "Bob's dog";
final myString = 'Bob\'s dog'; // ok
The Dart style guide appears to be silent on the issue.
Does the type of quotes matter when using use strict?
Actually in Javascript using double quotes or single quotes doesn't change anything.
It's more important you use the same option in your entire code.
Even if you use mixed quotes it will not change anything:
'use strict'
var myString = "double quotes string"
So using use strict
with double quotes or single quotes are the same.
In many libraries they commonly use one of them for strings and another to avoid escape, like in this example:
'A string that\'s single quoted'
"a string that's double quoted"
So specifically in English sometimes is useful using double quotes instead of single quotes to avoid this kind of escape thing.
Back-tick vs single quote in js
the back-tick allows you to use string templating for example:
var value = 4;
var str = `text with a ${value}`
// str will be : 'text with a 4'
for "
vs '
I say look at this post: https://stackoverflow.com/a/9959952/6739517
As for performance, it seems like it would be the same if you are just using back-ticks for plain strings. However when building strings it looks like concatenation is still the way to go. Take a look here:
2018 update: It seems that ES6 string templating can be faster than concatenation in some cases. Take a look at the following post for some hard numbers:
Are ES6 template literals faster than string concatenation?
2020 update: Generally speaking you should not be worried about performance when considering which type of quotation to use. There might be tiny differences but as many have pointed out these are such tiny improvements you are likely better off optimizing your code rather than considering which character to use. That said, this doesn't really answer the question of consistency. For me, I generally follow Airbnb's style guide. That is strings are always declared with single quotes (') unless you need to build a string then you should avoid concatenation and only use string templating with backticks (`). Finally, double quotes are reserved for JSON and HTML.
Related Topics
Set a Callback Function to a New Window in JavaScript
Iis 7.5 No Images CSS Js Showing
Jquery: How to Listen for Dom Changes
How to Format Vuetify Data Table Date Column
Angular2 Router 2.0.0 Not Reloading Components When Same Url Loaded with Different Parameters
When Using Jquery On(), Why Use (Document) VS. the Element Itself
How to Listen/Detect Changes to an Input Value - When the Input Value Is Changed via JavaScript
How to Reach the Element Itself Inside Jquery's 'Val'
Addeventlistener with Ajax Not Working Well
How to Prevent an Http Request Just for a Favicon
HTML - How to Show Tooltip Only When Ellipsis Is Activated
How to Subtract Date/Time in JavaScript
Jquery "Blinking Highlight" Effect on Div
Why Does JavaScript Replace Only First Instance When Using Replace